#24264: foreign key constraint error migrating integer pk to CharField
----------------------------+-------------------------------------
Reporter: timgraham | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.8alpha1
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+-------------------------------------
Comment (by MarkusH):
I managed to construct a test_case for this issue.
I made the FK span two apps to make sure it works across apps. Within the
same app it boils down to the same problem.
{{{#!diff
diff --git a/tests/migrations/migrations_test_apps/alter_fk/__init__.py
b/tests/migrations/migrations_test_apps/alter_fk/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/author_app/__init__.py
b/tests/migrations/migrations_test_apps/alter_fk/author_app/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/0001_initial.py
b/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/0001_initial.py
new file mode 100644
index 0000000..6017d16
--- /dev/null
+++
b/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/0001_initial.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Author',
+ fields=[
+ ('id', models.AutoField(serialize=False,
auto_created=True, primary_key=True)),
+ ('name', models.CharField(max_length=50)),
+ ],
+ ),
+ ]
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/0002_alter_id.py
b/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/0002_alter_id.py
new file mode 100644
index 0000000..0256b75
--- /dev/null
+++
b/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/0002_alter_id.py
@@ -0,0 +1,20 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('author_app', '0001_initial'),
+ ('book_app', '0001_initial'), # Forces the book table to alter
the FK
+ ]
+
+ operations = [
+ migrations.AlterField(
+ model_name='author',
+ name='id',
+ field=models.CharField(max_length=10, primary_key=True),
+ ),
+ ]
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/__init__.py
b/tests/migrations/migrations_test_apps/alter_fk/author_app/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/book_app/__init__.py
b/tests/migrations/migrations_test_apps/alter_fk/book_app/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/book_app/migrations/0001_initial.py
b/tests/migrations/migrations_test_apps/alter_fk/book_app/migrations/0001_initial.py
new file mode 100644
index 0000000..eb1862d
--- /dev/null
+++
b/tests/migrations/migrations_test_apps/alter_fk/book_app/migrations/0001_initial.py
@@ -0,0 +1,22 @@
+# -*- coding: utf-8 -*-
+from __future__ import unicode_literals
+
+from django.db import models, migrations
+
+
+class Migration(migrations.Migration):
+
+ dependencies = [
+ ('author_app', '0001_initial'),
+ ]
+
+ operations = [
+ migrations.CreateModel(
+ name='Book',
+ fields=[
+ ('id', models.AutoField(serialize=False,
auto_created=True, primary_key=True)),
+ ('title', models.CharField(max_length=50)),
+ ('author', models.ForeignKey('author_app.Author')),
+ ],
+ ),
+ ]
diff --git
a/tests/migrations/migrations_test_apps/alter_fk/book_app/migrations/__init__.py
b/tests/migrations/migrations_test_apps/alter_fk/book_app/migrations/__init__.py
new file mode 100644
index 0000000..e69de29
diff --git a/tests/migrations/test_executor.py
b/tests/migrations/test_executor.py
index 12385ac..30de553 100644
--- a/tests/migrations/test_executor.py
+++ b/tests/migrations/test_executor.py
@@ -368,6 +368,41 @@ class ExecutorTests(MigrationTestBase):
]
self.assertEqual(call_args_list, expected)
+ @override_settings(
+ INSTALLED_APPS=[
+ "migrations.migrations_test_apps.alter_fk.author_app",
+ "migrations.migrations_test_apps.alter_fk.book_app",
+ ]
+ )
+ def test_alter_id_type_with_fk(self):
+ try:
+ executor = MigrationExecutor(connection)
+ self.assertTableNotExists("author_app_author")
+ self.assertTableNotExists("book_app_book")
+ # Apply initial migrations
+ executor.migrate([
+ ("author_app", "0001_initial"),
+ ("book_app", "0001_initial"),
+ ])
+ self.assertTableExists("author_app_author")
+ self.assertTableExists("book_app_book")
+ # Rebuild the graph to reflect the new DB state
+ executor.loader.build_graph()
+
+ # Apply PK type alteration
+ executor.migrate([("author_app", "0002_alter_id")])
+
+ # Rebuild the graph to reflect the new DB state
+ executor.loader.build_graph()
+ finally:
+ # Cleanup
+ executor.migrate([
+ ("author_app", None),
+ ("book_app", None),
+ ])
+ self.assertTableNotExists("author_app_author")
+ self.assertTableNotExists("book_app_book")
+
class FakeLoader(object):
def __init__(self, graph, applied):
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/24264#comment:4>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.edf2c94c2f6fcf51a3bcf42bddc128b6%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.