Honor db_constraint=False in migration schema editor#2226
Open
youdie006 wants to merge 1 commit into
Open
Conversation
The migration schema editor always emitted the FOREIGN KEY reference for ForeignKeyField/ManyToManyField regardless of db_constraint, unlike the runtime schema generator. Guard FK emission in _get_fk_field_definition on fk_field.db_constraint, and add a no-constraint M2M through-table template, mirroring schema_generator. The plain column (and through table) are still emitted; only the FK constraint is dropped when db_constraint=False.
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
The migration schema editor always emitted the
FOREIGN KEYreference forForeignKeyField/ManyToManyField, ignoringdb_constraint=False— unlike the runtime schema generator, which respects it.grep db_constraint tortoise/migrations/returned zero matches, confirming the migration subsystem ignored the flag.Motivation and Context
Closes #2223. Models that set
db_constraint=False(to avoid a hard DB-level FK while keeping the ORM relation) still got a constrained FK in generated migrations, defeating the flag.How Has This Been Tested?
_get_fk_field_definition: guard the_get_fk_reference_string(...)append onfk_field.db_constraint(mirrorsbackends/base/schema_generator.py:264). Coverscreate_modelandadd_field._get_m2m_table_definition: a no-constraint through-table template selected whennot field.db_constraint(mirrorsschema_generator.py:328/370); the unique index is still emitted.tests/migrations/test_schema_editor_sql.py(FK increate_model, FK viaadd_field, M2M) assert noREFERENCES/FOREIGN KEYwhile the column remains — red→green. Full sqlite suite: 1912 passed, 0 failures.Checklist
CHANGELOG.rstupdatedruff check/ruff format --checkclean