From 283b630d6324c936cbe42d8f0169f056b3ba59c6 Mon Sep 17 00:00:00 2001 From: Jean-Louis Fuchs Date: Thu, 5 Mar 2015 13:41:15 +0100 Subject: [PATCH] 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 --- django/db/backends/schema.py | 6 +++--- docs/releases/1.7.6.txt | 3 +++ tests/schema/models.py | 9 +++++++++ tests/schema/tests.py | 36 ++++++++++++++++++++++++++++++++++-- 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/django/db/backends/schema.py b/django/db/backends/schema.py index 54e6e06c4f..4a5637c8da 100644 --- a/django/db/backends/schema.py +++ b/django/db/backends/schema.py @@ -675,9 +675,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 bcdb135b03..e326cfb440 100644 --- a/docs/releases/1.7.6.txt +++ b/docs/releases/1.7.6.txt @@ -11,3 +11,6 @@ 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/models.py b/tests/schema/models.py index 6eba7ccae1..b9331750c4 100644 --- a/tests/schema/models.py +++ b/tests/schema/models.py @@ -87,6 +87,15 @@ class BookWithM2M(models.Model): apps = new_apps +class BookWithoutFK(models.Model): + author = models.IntegerField() + title = models.CharField(max_length=100, db_index=True) + pub_date = models.DateTimeField() + + class Meta: + apps = new_apps + + class TagThrough(models.Model): book = models.ForeignKey("schema.BookWithM2MThrough") tag = models.ForeignKey("schema.TagM2MTest") diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 996cec103e..cc95d5539e 100644 --- a/tests/schema/tests.py +++ b/tests/schema/tests.py @@ -11,7 +11,7 @@ from .fields import CustomManyToManyField, InheritedManyToManyField from .models import (Author, AuthorWithDefaultHeight, AuthorWithM2M, Book, BookWithLongName, BookWithSlug, BookWithM2M, Tag, TagIndexed, TagM2MTest, TagUniqueRename, UniqueTest, Thing, TagThrough, BookWithM2MThrough, AuthorTag, AuthorWithM2MThrough, - AuthorWithEvenLongerName, BookWeak, Note, BookWithO2O) + AuthorWithEvenLongerName, BookWeak, Note, BookWithO2O, BookWithoutFK) class SchemaTests(TransactionTestCase): @@ -29,7 +29,7 @@ class SchemaTests(TransactionTestCase): Author, AuthorWithM2M, Book, BookWithLongName, BookWithSlug, BookWithM2M, Tag, TagIndexed, TagM2MTest, TagUniqueRename, UniqueTest, Thing, TagThrough, BookWithM2MThrough, AuthorWithEvenLongerName, - BookWeak, BookWithO2O, + BookWeak, BookWithO2O, BookWithoutFK, ] # Utility functions @@ -536,6 +536,38 @@ class SchemaTests(TransactionTestCase): else: 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 + """ + # Create the tables + with connection.schema_editor() as editor: + editor.create_model(Author) + editor.create_model(BookWithoutFK) + # Ensure no FK constraint exists + constraints = self.get_constraints(BookWithoutFK._meta.db_table) + for name, details in constraints.items(): + if details['foreign_key']: + self.fail('Found an unexpected FK constraint to %s' % details['columns']) + new_field = ForeignKey(Author) + new_field.set_attributes_from_name("author") + with connection.schema_editor() as editor: + editor.alter_field( + BookWithoutFK, + BookWithoutFK._meta.get_field_by_name("author")[0], + new_field, + strict=True, + ) + constraints = self.get_constraints(BookWithoutFK._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): """