Skip to content

Commit

Permalink
Fix error on using deprecated pk param
Browse files Browse the repository at this point in the history
  • Loading branch information
abondar committed May 24, 2024
1 parent 8e75f88 commit e7d4b99
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 6 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ Changelog
0.21
====

0.21.1
------
Fixed
^^^^^
- Fix error on using old style `pk=True`

0.21.0
------
Added
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -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 <andrey@bondar.ru>", "Nickolas Grigoriadis <nagrigoriadis@gmail.com>", "long2ice <long2ice@gmail.com>"]
license = "Apache-2.0"
Expand Down
12 changes: 10 additions & 2 deletions tests/test_basic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from tests.testmodels import Tournament
from tests.testmodels import OldStyleModel, Tournament
from tortoise.contrib import test


Expand All @@ -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"),
Expand All @@ -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
5 changes: 5 additions & 0 deletions tests/testmodels.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:]])
Expand Down
6 changes: 3 additions & 3 deletions tortoise/fields/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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)

Expand Down

0 comments on commit e7d4b99

Please sign in to comment.