Fix db_default on FK/O2O dropped during _init_relations#2200
Open
mixartemev wants to merge 1 commit into
Open
Conversation
When db_default is set on a ForeignKeyField/OneToOneField, the value never reaches the generated DDL: `_init_relations` builds the implicit `<fk>_id` key field by copying the related model's PK and patching a small set of attributes from the FK object, but `db_default` is not in that list. The schema editor then iterates fields_db_projection (which contains the key field) and finds has_db_default() == False, so the CREATE TABLE for the owning model has no DEFAULT clause on the FK column. Add `db_default` to the attribute-copy tuple so it propagates from the FK to the underlying column, mirroring how PR tortoise#2129 fixed the same omission for non-relation fields. Fixes tortoise#2199
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
_init_relationsbuilds the implicit<fk>_idkey field by copying the related model's PK field and then patching a small set of attributes from the FK object:db_defaultis missing from this list. The schema editor then iteratesfields_db_projection(which holds the key field, not the FK relation field) and inspectskey_field.has_db_default(). It returnsFalse, soCREATE TABLEfor the owning model is emitted without aDEFAULTclause on the FK column — even whendb_default=...is set on theForeignKeyField/OneToOneFieldand faithfully recorded in the generated migration.This fix adds
"db_default"to the attribute-copy tuple. Same path coversOneToOneFieldsince both go throughinit_fk_o2o_field.Motivation and Context
Fixes #2199
PR #2129 fixed the same omission for non-relation fields (the schema editor now emits
DEFAULTfor plain columns). This is the FK twin of that bug, living one layer up inApps._init_relations— the schema editor never gets a chance to see the default because it isn't on the key field.How Has This Been Tested?
Added
test_create_model_includes_db_default_on_fkintests/migrations/test_schema_editor_sql.py:Dcand a modelAppwithdc = ForeignKeyField("models.Dc", db_default=2).init_apps(Dc, App)(which exercisesStateApps._init_relations— the fixed path).create_model(App)via the schema editor."dc_id"andDEFAULT 2.The test fails on
developHEAD without the fix (verified locally), and passes with it. All 298 existing migration tests continue to pass.Checklist: