From 56f468681aaedefcdada1b86c441c12ae96e7fea Mon Sep 17 00:00:00 2001 From: Jacob Walls Date: Thu, 19 Jun 2025 06:22:23 -0400 Subject: Refs #35038 -- Reduced CreateModel/AlterConstraint operations when optimizing migrations. --- django/db/migrations/operations/models.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'django') diff --git a/django/db/migrations/operations/models.py b/django/db/migrations/operations/models.py index 93d5629ebe..1b241230df 100644 --- a/django/db/migrations/operations/models.py +++ b/django/db/migrations/operations/models.py @@ -346,6 +346,21 @@ class CreateModel(ModelOperation): }, ), ] + elif isinstance(operation, AlterConstraint): + options_constraints = [ + constraint + for constraint in self.options.get("constraints", []) + if constraint.name != operation.name + ] + [operation.constraint] + return [ + replace( + self, + options={ + **self.options, + "constraints": options_constraints, + }, + ), + ] elif isinstance(operation, RemoveConstraint): options_constraints = [ constraint -- cgit v1.3