summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJean-Louis Fuchs <ganwell@fangorn.ch>2015-03-05 13:41:15 +0100
committerMarkus Holtermann <info@markusholtermann.eu>2015-03-07 14:15:27 +0100
commit1ae2df6bfc3afadd7edb29a5887711f70cdb52c6 (patch)
tree8af72831b0e4f11bb32b87c1e2870585a634c5b9
parentac07890f959c467b3fc9c6dd6d36aafc2eff1fcc (diff)
[1.8.x] Fixed #24447 -- Made migrations add FK constraints for existing columns
When altering from e.g. an IntegerField to a ForeignKey, Django didn't add a constraint. Backport of f4f0060feaee6bbd76a0d575487682bc541111e4 from master
-rw-r--r--django/db/backends/base/schema.py6
-rw-r--r--docs/releases/1.7.6.txt6
-rw-r--r--tests/schema/tests.py39
3 files changed, 47 insertions, 4 deletions
diff --git a/django/db/backends/base/schema.py b/django/db/backends/base/schema.py
index d14f8919c0..d53836e62e 100644
--- a/django/db/backends/base/schema.py
+++ b/django/db/backends/base/schema.py
@@ -709,9 +709,9 @@ class BaseDatabaseSchemaEditor(object):
}
)
# Does it have a foreign key?
- if new_field.rel and \
- (fks_dropped or (old_field.rel and not old_field.db_constraint)) and \
- new_field.db_constraint:
+ if (new_field.rel and
+ (fks_dropped or not old_field.rel or not old_field.db_constraint) and
+ new_field.db_constraint):
self.execute(self._create_fk_sql(model, new_field, "_fk_%(to_table)s_%(to_column)s"))
# Rebuild FKs that pointed to us if we previously had to drop them
if old_field.primary_key and new_field.primary_key and old_type != new_type:
diff --git a/docs/releases/1.7.6.txt b/docs/releases/1.7.6.txt
index dd4412a783..e326cfb440 100644
--- a/docs/releases/1.7.6.txt
+++ b/docs/releases/1.7.6.txt
@@ -9,4 +9,8 @@ Django 1.7.6 fixes several bugs in 1.7.5.
Bugfixes
========
-* ...
+* Fixed crash when coercing ``ManyRelatedManager`` to a string
+ (:ticket:`24352`).
+
+* Fixed a bug that prevented migrations from adding a FK constraint
+ for an existing column. (:ticket:`24447`).
diff --git a/tests/schema/tests.py b/tests/schema/tests.py
index 901397e391..2f4a84db09 100644
--- a/tests/schema/tests.py
+++ b/tests/schema/tests.py
@@ -546,6 +546,45 @@ class SchemaTests(TransactionTestCase):
self.fail("No FK constraint for author_id found")
@unittest.skipUnless(connection.features.supports_foreign_keys, "No FK support")
+ def test_alter_to_fk(self):
+ """
+ #24447 - Tests adding a FK constraint for an existing column
+ """
+ class LocalBook(Model):
+ author = IntegerField()
+ title = CharField(max_length=100, db_index=True)
+ pub_date = DateTimeField()
+
+ class Meta:
+ app_label = 'schema'
+ apps = new_apps
+
+ self.local_models = [LocalBook]
+
+ # Create the tables
+ with connection.schema_editor() as editor:
+ editor.create_model(Author)
+ editor.create_model(LocalBook)
+ # Ensure no FK constraint exists
+ constraints = self.get_constraints(LocalBook._meta.db_table)
+ for name, details in constraints.items():
+ if details['foreign_key']:
+ self.fail('Found an unexpected FK constraint to %s' % details['columns'])
+ old_field = LocalBook._meta.get_field("author")
+ new_field = ForeignKey(Author)
+ new_field.set_attributes_from_name("author")
+ with connection.schema_editor() as editor:
+ editor.alter_field(LocalBook, old_field, new_field, strict=True)
+ constraints = self.get_constraints(LocalBook._meta.db_table)
+ # Ensure FK constraint exists
+ for name, details in constraints.items():
+ if details['foreign_key'] and details['columns'] == ["author_id"]:
+ self.assertEqual(details['foreign_key'], ('schema_author', 'id'))
+ break
+ else:
+ self.fail("No FK constraint for author_id found")
+
+ @unittest.skipUnless(connection.features.supports_foreign_keys, "No FK support")
def test_alter_o2o_to_fk(self):
"""
#24163 - Tests altering of OneToOneField to ForeignKey