Skip to content

Commit bc4f971

Browse files
authored
Fix db_default on FK/O2O dropped during _init_relations (#2200)
1 parent 5c79e51 commit bc4f971

3 files changed

Lines changed: 35 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+
- ``db_default`` on ``ForeignKeyField``/``OneToOneField`` now propagates to the underlying ``<fk>_id`` column, so ``CREATE TABLE`` emits the ``DEFAULT`` clause for FK columns. (#2199)
2425
- ``MigrationRecorder`` no longer emits tortoise's own ``pk`` field ``DeprecationWarning`` when applying migrations; it now builds its bookkeeping model with ``primary_key=True``. (#2203)
2526
- ``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)
2627
- 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)

tests/migrations/test_schema_editor_sql.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,36 @@ class Meta:
249249
sql = client.executed[0]
250250
assert 'CREATE TABLE "widget"' in sql
251251
assert "DEFAULT 'active'" in sql
252+
253+
254+
@pytest.mark.asyncio
255+
async def test_create_model_includes_db_default_on_fk() -> None:
256+
"""CreateModel should include DEFAULT clause for FK columns with db_default."""
257+
258+
class Dc(Model):
259+
id = fields.IntField(primary_key=True)
260+
261+
class Meta:
262+
table = "dc"
263+
app = "models"
264+
265+
class App(Model):
266+
id = fields.IntField(primary_key=True)
267+
dc: fields.ForeignKeyRelation[Dc] = fields.ForeignKeyField("models.Dc", db_default=2)
268+
269+
class Meta:
270+
table = "app"
271+
app = "models"
272+
273+
init_apps(Dc, App)
274+
275+
client = FakeClient("sql")
276+
editor = TestSchemaEditor(client)
277+
278+
await editor.create_model(App)
279+
280+
assert len(client.executed) == 1
281+
sql = client.executed[0]
282+
assert 'CREATE TABLE "app"' in sql
283+
assert '"dc_id"' in sql
284+
assert "DEFAULT 2" in sql

tortoise/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ def init_fk_o2o_field(model: type[Model], field: str, is_o2o: bool = False) -> N
192192
key_field = f"{field}_id"
193193
key_fk_object.reference = fk_object
194194
key_fk_object.source_field = fk_object.source_field or key_field
195-
for attr in ("index", "default", "null", "generated", "description"):
195+
for attr in ("index", "default", "null", "generated", "description", "db_default"):
196196
setattr(key_fk_object, attr, getattr(fk_object, attr))
197197
if is_o2o:
198198
key_fk_object.pk = fk_object.pk

0 commit comments

Comments
 (0)