diff options
| author | Jean-Louis Fuchs <ganwell@fangorn.ch> | 2015-03-05 13:41:15 +0100 |
|---|---|---|
| committer | Markus Holtermann <info@markusholtermann.eu> | 2015-03-07 14:09:56 +0100 |
| commit | f4f0060feaee6bbd76a0d575487682bc541111e4 (patch) | |
| tree | 2ea7605a79863e91bbdf9cb864feb0ad87f2fc0d /tests/schema | |
| parent | c36b60836be9e78ab877566cc43dc03960ce944d (diff) | |
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.
Diffstat (limited to 'tests/schema')
| -rw-r--r-- | tests/schema/tests.py | 39 |
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/schema/tests.py b/tests/schema/tests.py index 2bc47ec069..df45ed2d59 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 |
