Skip to content

Commit 5c79e51

Browse files
🐛 Stop recorder emitting tortoise's own pk warning (#2203)
1 parent 5ec4cfa commit 5c79e51

3 files changed

Lines changed: 20 additions & 1 deletion

File tree

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Added
2121
Fixed
2222
^^^^^
2323
- ``MigrationRecorder`` now uses parameterized queries; fixes MariaDB/MySQL rejecting ISO-8601 ``applied_at`` values. (#2132)
24+
- ``MigrationRecorder`` no longer emits tortoise's own ``pk`` field ``DeprecationWarning`` when applying migrations; it now builds its bookkeeping model with ``primary_key=True``. (#2203)
2425
- ``QuerySet.count()`` now matches the limited query result for the LIMIT/OFFSET edge cases: it returns ``0`` (instead of a negative number) when ``offset()`` exceeds the total row count, and ``0`` (instead of the total) for ``limit(0)``. (#2208)
2526
- Field declarations on models now resolve to their concrete type (e.g. ``CharField[str]``) in Pyright/Pylance instead of ``Field[Unknown]``; the ``Field.__new__`` type-check stub now returns ``Self``. (#2216)
2627

tests/migrations/test_recorder.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import warnings
2+
13
import pytest
24

35
from tortoise.backends.base.client import Capabilities
@@ -126,6 +128,22 @@ async def test_recorder_sqlite_placeholders() -> None:
126128
assert '"name" = ?' in delete_query
127129

128130

131+
def test_recorder_model_emits_no_field_deprecation_warnings() -> None:
132+
"""Regression: the migration recorder must not trip tortoise's own
133+
``pk``/``index`` field deprecation warnings. It builds its bookkeeping
134+
model internally, so downstream projects can't silence them.
135+
"""
136+
with warnings.catch_warnings(record=True) as caught:
137+
# Reset filters so the repo-wide ``ignore:`pk` deprecation`` filter in
138+
# pyproject.toml doesn't mask a regression here.
139+
warnings.simplefilter("always")
140+
MigrationRecorder(FakeConnection("sqlite"))
141+
142+
messages = [str(w.message) for w in caught if issubclass(w.category, DeprecationWarning)]
143+
assert not any("`pk` is deprecated" in m for m in messages), messages
144+
assert not any("`index` is deprecated" in m for m in messages), messages
145+
146+
129147
@pytest.mark.asyncio
130148
async def test_recorder_oracle_placeholders() -> None:
131149
connection = FakeConnection("oracle")

tortoise/migrations/recorder.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def _placeholder(self, pos: int) -> str:
3535

3636
def _make_model(self, table_name: str) -> type[Model]:
3737
class MigrationRecord(Model):
38-
id = fields.IntField(pk=True)
38+
id = fields.IntField(primary_key=True)
3939
app = fields.CharField(max_length=255)
4040
name = fields.CharField(max_length=255)
4141
applied_at = fields.DatetimeField()

0 commit comments

Comments
 (0)