mirror of
https://github.com/django/django.git
synced 2025-10-25 06:36:07 +00:00
[1.7.x] Fixed #23415 -- Added fields for unmanaged and proxy model migrations.
Thanks sky-chen for the report.
Backport of 215aa4f53b from master
This commit is contained in:
committed by
Tim Graham
parent
b5d6092707
commit
00c2a721e2
@@ -31,6 +31,7 @@ class AutodetectorTests(TestCase):
|
||||
author_name_default = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default='Ada Lovelace'))])
|
||||
author_name_deconstructable_1 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
|
||||
author_name_deconstructable_2 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default=DeconstructableObject()))])
|
||||
author_custom_pk = ModelState("testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))])
|
||||
author_with_book = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
||||
author_with_book_order_wrt = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))], options={"order_with_respect_to": "book"})
|
||||
author_renamed_with_book = ModelState("testapp", "Writer", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
||||
@@ -44,6 +45,10 @@ class AutodetectorTests(TestCase):
|
||||
author_proxy_proxy = ModelState("testapp", "AAuthorProxyProxy", [], {"proxy": True}, ("testapp.authorproxy", ))
|
||||
author_unmanaged = ModelState("testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author", ))
|
||||
author_unmanaged_managed = ModelState("testapp", "AuthorUnmanaged", [], {}, ("testapp.author", ))
|
||||
author_unmanaged_default_pk = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
|
||||
author_unmanaged_custom_pk = ModelState("testapp", "Author", [
|
||||
("pk_field", models.IntegerField(primary_key=True)),
|
||||
])
|
||||
author_with_m2m = ModelState("testapp", "Author", [
|
||||
("id", models.AutoField(primary_key=True)),
|
||||
("publishers", models.ManyToManyField("testapp.Publisher")),
|
||||
@@ -644,6 +649,24 @@ class AutodetectorTests(TestCase):
|
||||
self.assertOperationAttributes(changes, "testapp", 0, 0, name="AuthorProxy")
|
||||
self.assertOperationAttributes(changes, "testapp", 0, 1, name="AuthorProxy", options={})
|
||||
|
||||
def test_proxy_custom_pk(self):
|
||||
"#23415 - The autodetector must correctly deal with custom FK on proxy models."
|
||||
# First, we test the default pk field name
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.author_empty, self.author_proxy_third, self.book_proxy_fk])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
# The field name the FK on the book model points to
|
||||
self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'id')
|
||||
|
||||
# Now, we test the custom pk field name
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.author_custom_pk, self.author_proxy_third, self.book_proxy_fk])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
# The field name the FK on the book model points to
|
||||
self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'pk_field')
|
||||
|
||||
def test_unmanaged(self):
|
||||
"Tests that the autodetector correctly deals with managed models"
|
||||
# First, we test adding an unmanaged model
|
||||
@@ -667,6 +690,24 @@ class AutodetectorTests(TestCase):
|
||||
self.assertOperationAttributes(changes, 'testapp', 0, 0, name="AuthorUnmanaged")
|
||||
self.assertOperationAttributes(changes, 'testapp', 0, 1, name="AuthorUnmanaged")
|
||||
|
||||
def test_unmanaged_custom_pk(self):
|
||||
"#23415 - The autodetector must correctly deal with custom FK on unmanaged models."
|
||||
# First, we test the default pk field name
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.author_unmanaged_default_pk, self.book])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
# The field name the FK on the book model points to
|
||||
self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'id')
|
||||
|
||||
# Now, we test the custom pk field name
|
||||
before = self.make_project_state([])
|
||||
after = self.make_project_state([self.author_unmanaged_custom_pk, self.book])
|
||||
autodetector = MigrationAutodetector(before, after)
|
||||
changes = autodetector._detect_changes()
|
||||
# The field name the FK on the book model points to
|
||||
self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].rel.field_name, 'pk_field')
|
||||
|
||||
@override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
|
||||
def test_swappable(self):
|
||||
before = self.make_project_state([self.custom_user])
|
||||
|
||||
Reference in New Issue
Block a user