Fixed #373 -- Added CompositePrimaryKey.

Thanks Lily Foote and Simon Charette for reviews and mentoring
this Google Summer of Code 2024 project.

Co-authored-by: Simon Charette <charette.s@gmail.com>
Co-authored-by: Lily Foote <code@lilyf.org>
This commit is contained in:
Bendeguz Csirmaz 2024-04-07 10:32:16 +08:00 committed by Sarah Boyce
parent 86661f2449
commit 978aae4334
43 changed files with 3078 additions and 29 deletions

View file

@ -5059,6 +5059,95 @@ class AutodetectorTests(BaseAutodetectorTests):
self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
self.assertOperationAttributes(changes, "testapp", 0, 0, name="Book")
@mock.patch(
"django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition"
)
def test_add_composite_pk(self, mocked_ask_method):
before = [
ModelState(
"app",
"foo",
[
("id", models.AutoField(primary_key=True)),
],
),
]
after = [
ModelState(
"app",
"foo",
[
("pk", models.CompositePrimaryKey("foo_id", "bar_id")),
("id", models.IntegerField()),
],
),
]
changes = self.get_changes(before, after)
self.assertEqual(mocked_ask_method.call_count, 0)
self.assertNumberMigrations(changes, "app", 1)
self.assertOperationTypes(changes, "app", 0, ["AddField", "AlterField"])
self.assertOperationAttributes(
changes,
"app",
0,
0,
name="pk",
model_name="foo",
preserve_default=True,
)
self.assertOperationAttributes(
changes,
"app",
0,
1,
name="id",
model_name="foo",
preserve_default=True,
)
def test_remove_composite_pk(self):
before = [
ModelState(
"app",
"foo",
[
("pk", models.CompositePrimaryKey("foo_id", "bar_id")),
("id", models.IntegerField()),
],
),
]
after = [
ModelState(
"app",
"foo",
[
("id", models.AutoField(primary_key=True)),
],
),
]
changes = self.get_changes(before, after)
self.assertNumberMigrations(changes, "app", 1)
self.assertOperationTypes(changes, "app", 0, ["RemoveField", "AlterField"])
self.assertOperationAttributes(
changes,
"app",
0,
0,
name="pk",
model_name="foo",
)
self.assertOperationAttributes(
changes,
"app",
0,
1,
name="id",
model_name="foo",
preserve_default=True,
)
class MigrationSuggestNameTests(SimpleTestCase):
def test_no_operations(self):