From e7d4b9977971ddf89a55d41525420f89008ab2d8 Mon Sep 17 00:00:00 2001 From: abondar Date: Fri, 24 May 2024 17:44:24 +0300 Subject: [PATCH] Fix error on using deprecated pk param --- CHANGELOG.rst | 6 ++++++ pyproject.toml | 2 +- tests/test_basic.py | 12 ++++++++++-- tests/testmodels.py | 5 +++++ tortoise/fields/data.py | 6 +++--- 5 files changed, 25 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 220a581af..4a8fb5fb6 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -9,6 +9,12 @@ Changelog 0.21 ==== +0.21.1 +------ +Fixed +^^^^^ +- Fix error on using old style `pk=True` + 0.21.0 ------ Added diff --git a/pyproject.toml b/pyproject.toml index f49a2c19e..0281ea0e6 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,6 +1,6 @@ [tool.poetry] name = "tortoise-orm" -version = "0.21.0" +version = "0.21.1" description = "Easy async ORM for python, built with relations in mind" authors = ["Andrey Bondar ", "Nickolas Grigoriadis ", "long2ice "] license = "Apache-2.0" diff --git a/tests/test_basic.py b/tests/test_basic.py index c6491badd..189897776 100644 --- a/tests/test_basic.py +++ b/tests/test_basic.py @@ -1,4 +1,4 @@ -from tests.testmodels import Tournament +from tests.testmodels import OldStyleModel, Tournament from tortoise.contrib import test @@ -10,7 +10,8 @@ async def test_basic(self): self.assertEqual(saved_event.id, tournament.id) await Tournament(name="Test 2").save() self.assertEqual( - await Tournament.all().values_list("id", flat=True), [tournament.id, tournament.id + 1] + await Tournament.all().values_list("id", flat=True), + [tournament.id, tournament.id + 1], ) self.assertListSortEqual( await Tournament.all().values("id", "name"), @@ -20,3 +21,10 @@ async def test_basic(self): ], sorted_key="id", ) + + async def test_basic_oldstyle(self): + obj = await OldStyleModel.create(name="Test") + assert obj.pk + + assert OldStyleModel._meta.fields_map["id"].pk + assert OldStyleModel._meta.fields_map["name"].index diff --git a/tests/testmodels.py b/tests/testmodels.py index a1021096c..e6ac6d87f 100644 --- a/tests/testmodels.py +++ b/tests/testmodels.py @@ -868,6 +868,11 @@ class Pair(Model): ) +class OldStyleModel(Model): + id = fields.IntField(pk=True) + name = fields.CharField(max_length=255, index=True) + + def camelize_var(var_name: str): var_parts: List[str] = var_name.split("_") return var_parts[0] + "".join([part.title() for part in var_parts[1:]]) diff --git a/tortoise/fields/data.py b/tortoise/fields/data.py index 7343c6437..af16c6b5b 100644 --- a/tortoise/fields/data.py +++ b/tortoise/fields/data.py @@ -74,7 +74,7 @@ class IntField(Field[int], int): allows_generated = True def __init__(self, primary_key: Optional[bool] = None, **kwargs: Any) -> None: - if primary_key: + if primary_key or kwargs.get("pk"): kwargs["generated"] = bool(kwargs.get("generated", True)) super().__init__(primary_key=primary_key, **kwargs) @@ -113,7 +113,7 @@ class BigIntField(Field[int], int): allows_generated = True def __init__(self, primary_key: Optional[bool] = None, **kwargs: Any) -> None: - if primary_key: + if primary_key or kwargs.get("pk"): kwargs["generated"] = bool(kwargs.get("generated", True)) super().__init__(primary_key=primary_key, **kwargs) @@ -153,7 +153,7 @@ class SmallIntField(Field[int], int): allows_generated = True def __init__(self, primary_key: Optional[bool] = None, **kwargs: Any) -> None: - if primary_key: + if primary_key or kwargs.get("pk"): kwargs["generated"] = bool(kwargs.get("generated", True)) super().__init__(primary_key=primary_key, **kwargs)