fix(migrations): emit Meta.constraints in CREATE TABLE (#2272) - #2275
jagadeepmamidi wants to merge 3 commits into
Conversation
Named UniqueConstraint and CheckConstraint were stored on CreateModel but never applied when creating tables, and MySQL add_constraint ignored custom names.
|
Great efficient work 🏅 |
|
@seejah please let me know if I can check any other issues too (happy to help) |
waketzheng
left a comment
There was a problem hiding this comment.
For _partial_unique_index_sqls(): the base class could just return [], and the
actual logic should move to an override in
tortoise/backends/base_postgres/schema_generator.py. Keeping self.DIALECT != "postgres"
in the base generator couples it to a specific dialect — new dialects should extend via
override, not by adding string checks here.
With that change, the MySQL override of _partial_unique_index_sqls() also becomes
unnecessary, since it would inherit the base return [] and correctly produce nothing.
|
Thanks for the feedback. I moved partial unique-index generation out of the base schema classes and into the PostgreSQL-specific implementations. The base classes now I also added regression coverage for PostgreSQL |
Named UniqueConstraint and CheckConstraint were stored on CreateModel but never applied when creating tables, and MySQL add_constraint ignored custom names.
Description
Meta.constraints(UniqueConstraint/CheckConstraint) are now emitted inCREATE TABLEfor bothtortoise migrateandgenerate_schemas.Previously
makemigrationsstored them onCreateModel, but table creation only appliedunique_together, so a new model never gotAddConstraintand named uniques likeunique_likenever appeared. MySQLadd_constraintalso ignorednameand generateduidx_*instead.CREATE TABLEcondition) becomeCREATE UNIQUE INDEX ... WHEREon PostgreSQLUniqueConstraintwhose resolved columns already matchunique_togetheris skippedadd_constrainthonorsconstraint.namewhen setMotivation and Context
Fixes #2272
Reporter used
tortoise makemigrations/tortoise migrateon MySQL 8.4 with:Expected a unique index named
unique_like; nothing was created, with no error.How Has This Been Tested?
Added regression tests in
tests/migrations/test_schema_editor_sql.pyandtests/migrations/test_schema_editor_constraints.pycovering:CreateModelSQL for named unique + check constraints (ANSI and MySQL)UniqueConstraintduplicatesunique_together(including FK field vsorganization_idcolumns)generate_schemasincludingMeta.constraintsadd_constraintusing the custom nameRan:
(52 passed)
Checklist: