Opened 42 minutes ago
#37553 assigned Bug
Adding a GeneratedField makes its migration depend on fields added to other models and apps
| Reported by: | Konstantinos Giannopoulos | Owned by: | Konstantinos Giannopoulos |
|---|---|---|---|
| Component: | Migrations | Version: | 5.0 |
| Severity: | Normal | Keywords: | GeneratedField, autodetector, dependencies |
| Cc: | Konstantinos Giannopoulos | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
When a GeneratedField is added to an existing model, the autodetector makes its AddField depend on fields added in the same makemigrations run on other models, including models in other apps. As a result, the app gets an unnecessary dependency on another app's migration, and its changes can be split into several migrations.
Initial models (migrated):
# catalog/models.py class Product(models.Model): price = models.DecimalField(max_digits=8, decimal_places=2) shipping_cost = models.DecimalField(max_digits=8, decimal_places=2, default=0) # orders/models.py class Customer(models.Model): name = models.CharField(max_length=200) class Order(models.Model): reference = models.CharField(max_length=20)
Add a GeneratedField that only uses Product's own fields, and, in the same change, an unrelated foreign key in the orders app:
# catalog/models.py class Product(models.Model): price = models.DecimalField(max_digits=8, decimal_places=2) shipping_cost = models.DecimalField(max_digits=8, decimal_places=2, default=0) total_price = models.GeneratedField( expression=models.F("price") + models.F("shipping_cost"), output_field=models.DecimalField(max_digits=9, decimal_places=2), db_persist=True, ) # orders/models.py class Order(models.Model): reference = models.CharField(max_length=20) customer = models.ForeignKey(Customer, models.PROTECT, null=True)
makemigrations generates catalog/migrations/0002_product_total_price.py with:
dependencies = [
("catalog", "0001_initial"),
("orders", "0002_order_customer"),
]
The dependency on orders is not needed: total_price only uses price and shipping_cost, which already exist.
If the same run also adds a model to catalog that orders refers to (for example a new catalog.Supplier and a new orders.PurchaseOrder.supplier foreign key), catalog's changes are split into 0002_supplier and 0003_product_total_price, with 0003 depending on the orders migration. migrate catalog then also runs the orders migration.
MigrationAutodetector._get_dependencies_for_generated_field() iterates over self.new_field_keys - self.old_field_keys, i.e. every field added anywhere in the project, and adds a dependency for each new relation field and each new field whose name appears in the expression, without checking that the field belongs to the generated field's model. Since a GeneratedField expression can only refer to fields of its own model, only fields added to that model are relevant.
Regression in 9aeb38c296c69532c7e64b5e3d706a5eb17b3f12 (#35359, Django 5.0.5). Before that commit, no cross-app dependency was added in this case.
A fix is to pass the generated field's app_label and model_name to _get_dependencies_for_generated_field() and only consider fields added to that model. Relation fields on the same model must still be considered (an expression can use e.g. publisher_id, see test_add_fk_before_generated_field). Regression tests for tests/migrations/test_autodetector.py:
def test_add_generated_field_ignores_fields_added_to_other_models(self): product = ModelState( "catalog", "Product", [("id", models.AutoField(primary_key=True)), ("price", models.IntegerField())], ) product_with_generated_field = ModelState( "catalog", "Product", [("id", models.AutoField(primary_key=True)), ("price", models.IntegerField()), ("double_price", models.GeneratedField( expression=models.F("price") * 2, output_field=models.IntegerField(), db_persist=True, ))], ) customer = ModelState("orders", "Customer", [("id", models.AutoField(primary_key=True))]) order = ModelState("orders", "Order", [("id", models.AutoField(primary_key=True))]) order_with_new_fields = ModelState( "orders", "Order", [("id", models.AutoField(primary_key=True)), ("customer", models.ForeignKey("orders.Customer", models.CASCADE, null=True)), ("price", models.IntegerField(default=0))], ) changes = self.get_changes( [product, customer, order], [product_with_generated_field, customer, order_with_new_fields], ) self.assertNumberMigrations(changes, "catalog", 1) self.assertOperationTypes(changes, "catalog", 0, ["AddField"]) self.assertMigrationDependencies(changes, "catalog", 0, []) def test_add_generated_field_not_split_by_other_app_dependency(self): product = ModelState( "catalog", "Product", [("id", models.AutoField(primary_key=True)), ("price", models.IntegerField())], ) product_with_generated_field = ModelState( "catalog", "Product", [("id", models.AutoField(primary_key=True)), ("price", models.IntegerField()), ("double_price", models.GeneratedField( expression=models.F("price") * 2, output_field=models.IntegerField(), db_persist=True, ))], ) supplier = ModelState("catalog", "Supplier", [("id", models.AutoField(primary_key=True))]) order = ModelState("orders", "Order", [("id", models.AutoField(primary_key=True))]) order_with_supplier = ModelState( "orders", "Order", [("id", models.AutoField(primary_key=True)), ("supplier", models.ForeignKey("catalog.Supplier", models.CASCADE, null=True))], ) changes = self.get_changes( [product, order], [product_with_generated_field, supplier, order_with_supplier], ) self.assertNumberMigrations(changes, "catalog", 1) self.assertOperationTypes(changes, "catalog", 0, ["CreateModel", "AddField"]) self.assertMigrationDependencies(changes, "orders", 0, [("catalog", "auto_1")])
A small project reproducing this end to end (including the split migrations) is attached.
Also happy to take this on if the ticket is approved.