mirror of https://github.com/django/django.git
Fixed #22030 -- Don't assume that all fields have a swappable_setting attribute.
This commit is contained in:
parent
c3434fed5b
commit
6873eeeefb
|
@ -233,7 +233,8 @@ class MigrationAutodetector(object):
|
||||||
field=field,
|
field=field,
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
swappable_setting = new_apps.get_model(app_label, model_name)._meta.get_field_by_name(field_name)[0].swappable_setting
|
new_field = new_apps.get_model(app_label, model_name)._meta.get_field_by_name(field_name)[0]
|
||||||
|
swappable_setting = getattr(new_field, 'swappable_setting', None)
|
||||||
if swappable_setting is not None:
|
if swappable_setting is not None:
|
||||||
self.add_swappable_dependency(app_label, swappable_setting)
|
self.add_swappable_dependency(app_label, swappable_setting)
|
||||||
# Old fields
|
# Old fields
|
||||||
|
|
|
@ -16,6 +16,7 @@ class AutodetectorTests(TestCase):
|
||||||
author_name = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200))])
|
author_name = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200))])
|
||||||
author_name_longer = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=400))])
|
author_name_longer = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=400))])
|
||||||
author_name_renamed = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("names", models.CharField(max_length=200))])
|
author_name_renamed = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("names", models.CharField(max_length=200))])
|
||||||
|
author_name_default = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200, default='Ada Lovelace'))])
|
||||||
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 = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("book", models.ForeignKey("otherapp.Book"))])
|
||||||
author_with_publisher = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher", models.ForeignKey("testapp.Publisher"))])
|
author_with_publisher = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("publisher", models.ForeignKey("testapp.Publisher"))])
|
||||||
author_with_custom_user = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("user", models.ForeignKey("thirdapp.CustomUser"))])
|
author_with_custom_user = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True)), ("name", models.CharField(max_length=200)), ("user", models.ForeignKey("thirdapp.CustomUser"))])
|
||||||
|
@ -369,3 +370,22 @@ class AutodetectorTests(TestCase):
|
||||||
# Check the dependency is correct
|
# Check the dependency is correct
|
||||||
migration = changes['testapp'][0]
|
migration = changes['testapp'][0]
|
||||||
self.assertEqual(migration.dependencies, [("__setting__", "AUTH_USER_MODEL")])
|
self.assertEqual(migration.dependencies, [("__setting__", "AUTH_USER_MODEL")])
|
||||||
|
|
||||||
|
def test_add_field_with_default(self):
|
||||||
|
"""
|
||||||
|
Adding a field with a default should work (#22030).
|
||||||
|
"""
|
||||||
|
# Make state
|
||||||
|
before = self.make_project_state([self.author_empty])
|
||||||
|
after = self.make_project_state([self.author_name_default])
|
||||||
|
autodetector = MigrationAutodetector(before, after)
|
||||||
|
changes = autodetector._detect_changes()
|
||||||
|
# Right number of migrations?
|
||||||
|
self.assertEqual(len(changes['testapp']), 1)
|
||||||
|
# Right number of actions?
|
||||||
|
migration = changes['testapp'][0]
|
||||||
|
self.assertEqual(len(migration.operations), 1)
|
||||||
|
# Right action?
|
||||||
|
action = migration.operations[0]
|
||||||
|
self.assertEqual(action.__class__.__name__, "AddField")
|
||||||
|
self.assertEqual(action.name, "name")
|
||||||
|
|
Loading…
Reference in New Issue