You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When db_default is set on a ForeignKeyField / OneToOneField, the value is silently dropped at apply time and never reaches the generated DDL. The CREATE TABLE for the owning model is emitted without a DEFAULT clause on the FK column, even though the migration file faithfully records db_default=... on the relation.
Note: #2117 / PR #2129 fixed this for regular fields (the schema editor now emits DEFAULT for non-relation columns). This issue is the FK twin of that bug, in a different code path.
Root cause
Apps._init_relations (tortoise/apps.py) builds the implicit key field (<fk>_id) by copying the related model's PK field and then patching a small set of attributes from the FK object:
db_default is not in this list. Since the schema editor iterates fields_db_projection (which contains the key field, not the relation field) and inspects key_field.has_db_default(), the DEFAULT clause is never produced for FK columns.
i.e. db_default on FK/O2O propagates to the underlying column just like it does for plain fields after #2129.
Additional context
Confirmed present in 1.1.7 (release) and develop (HEAD as of today).
Fix is a one-line change in _init_relations: add "db_default" to the attribute-copy tuple. Tests can mirror test_create_model_includes_db_default from PR Fix CreateModel not including DEFAULT clause for db_default fields #2129, but with a ForeignKeyField(..., db_default=<id>) instead of a plain field.
Same path covers OneToOneField since it shares the init_fk_o2o_field codepath.
Describe the bug
When
db_defaultis set on aForeignKeyField/OneToOneField, the value is silently dropped at apply time and never reaches the generated DDL. TheCREATE TABLEfor the owning model is emitted without aDEFAULTclause on the FK column, even though the migration file faithfully recordsdb_default=...on the relation.Note: #2117 / PR #2129 fixed this for regular fields (the schema editor now emits
DEFAULTfor non-relation columns). This issue is the FK twin of that bug, in a different code path.Root cause
Apps._init_relations(tortoise/apps.py) builds the implicit key field (<fk>_id) by copying the related model's PK field and then patching a small set of attributes from the FK object:db_defaultis not in this list. Since the schema editor iteratesfields_db_projection(which contains the key field, not the relation field) and inspectskey_field.has_db_default(), the DEFAULT clause is never produced for FK columns.To Reproduce
makemigrationsproduces an op that recordsdb_default=2on the FK:But
tortoise upgradeemits:— no
DEFAULT 2.Expected behavior
i.e.
db_defaulton FK/O2O propagates to the underlying column just like it does for plain fields after #2129.Additional context
1.1.7(release) anddevelop(HEAD as of today)._init_relations: add"db_default"to the attribute-copy tuple. Tests can mirrortest_create_model_includes_db_defaultfrom PR Fix CreateModel not including DEFAULT clause for db_default fields #2129, but with aForeignKeyField(..., db_default=<id>)instead of a plain field.OneToOneFieldsince it shares theinit_fk_o2o_fieldcodepath.