mirror of
https://github.com/django/django.git
synced 2025-08-04 10:59:45 +00:00
Fixed #35359 -- Fixed migration operations ordering when adding fields referenced by GeneratedField.expression.
Thank you to Simon Charette for the review.
This commit is contained in:
parent
97d48cd3c6
commit
9aeb38c296
4 changed files with 153 additions and 1 deletions
|
@ -13,6 +13,7 @@ from django.db.migrations.graph import MigrationGraph
|
|||
from django.db.migrations.loader import MigrationLoader
|
||||
from django.db.migrations.questioner import MigrationQuestioner
|
||||
from django.db.migrations.state import ModelState, ProjectState
|
||||
from django.db.models.functions import Concat, Lower
|
||||
from django.test import SimpleTestCase, TestCase, override_settings
|
||||
from django.test.utils import isolate_lru_cache
|
||||
|
||||
|
@ -1369,6 +1370,82 @@ class AutodetectorTests(BaseAutodetectorTests):
|
|||
self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True)
|
||||
self.assertEqual(mocked_ask_method.call_count, 3)
|
||||
|
||||
def test_add_field_before_generated_field(self):
|
||||
initial_state = ModelState(
|
||||
"testapp",
|
||||
"Author",
|
||||
[
|
||||
("name", models.CharField(max_length=20)),
|
||||
],
|
||||
)
|
||||
updated_state = ModelState(
|
||||
"testapp",
|
||||
"Author",
|
||||
[
|
||||
("name", models.CharField(max_length=20)),
|
||||
("surname", models.CharField(max_length=20)),
|
||||
(
|
||||
"lower_full_name",
|
||||
models.GeneratedField(
|
||||
expression=Concat(Lower("name"), Lower("surname")),
|
||||
output_field=models.CharField(max_length=30),
|
||||
db_persist=True,
|
||||
),
|
||||
),
|
||||
],
|
||||
)
|
||||
changes = self.get_changes([initial_state], [updated_state])
|
||||
self.assertNumberMigrations(changes, "testapp", 1)
|
||||
self.assertOperationTypes(changes, "testapp", 0, ["AddField", "AddField"])
|
||||
self.assertOperationFieldAttributes(
|
||||
changes, "testapp", 0, 1, expression=Concat(Lower("name"), Lower("surname"))
|
||||
)
|
||||
|
||||
def test_add_fk_before_generated_field(self):
|
||||
initial_state = ModelState(
|
||||
"testapp",
|
||||
"Author",
|
||||
[
|
||||
("name", models.CharField(max_length=20)),
|
||||
],
|
||||
)
|
||||
updated_state = [
|
||||
ModelState(
|
||||
"testapp",
|
||||
"Publisher",
|
||||
[
|
||||
("name", models.CharField(max_length=20)),
|
||||
],
|
||||
),
|
||||
ModelState(
|
||||
"testapp",
|
||||
"Author",
|
||||
[
|
||||
("name", models.CharField(max_length=20)),
|
||||
(
|
||||
"publisher",
|
||||
models.ForeignKey("testapp.Publisher", models.CASCADE),
|
||||
),
|
||||
(
|
||||
"lower_full_name",
|
||||
models.GeneratedField(
|
||||
expression=Concat("name", "publisher_id"),
|
||||
output_field=models.CharField(max_length=20),
|
||||
db_persist=True,
|
||||
),
|
||||
),
|
||||
],
|
||||
),
|
||||
]
|
||||
changes = self.get_changes([initial_state], updated_state)
|
||||
self.assertNumberMigrations(changes, "testapp", 1)
|
||||
self.assertOperationTypes(
|
||||
changes, "testapp", 0, ["CreateModel", "AddField", "AddField"]
|
||||
)
|
||||
self.assertOperationFieldAttributes(
|
||||
changes, "testapp", 0, 2, expression=Concat("name", "publisher_id")
|
||||
)
|
||||
|
||||
def test_remove_field(self):
|
||||
"""Tests autodetection of removed fields."""
|
||||
changes = self.get_changes([self.author_name], [self.author_empty])
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue