Opened 10 years ago
Closed 10 years ago
#23479 closed Uncategorized (duplicate)
makemigrations geterates wrong migration for ForeignKey to unmanaged model
Reported by: | Dmitri Bogomolov | Owned by: | nobody |
---|---|---|---|
Component: | Migrations | Version: | 1.7 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
I wrote a simple test app:
from django.db import models class Obj(models.Model): custom_named_id = models.IntegerField( primary_key=True, db_column='custom_named_id' # not needed and doesn't help ) field_2 = models.CharField(max_length=10, null=True, blank=True) class Meta: managed = False class Pointer(models.Model): name = models.CharField(max_length=10, null=True, blank=True) obj = models.ForeignKey('Obj')
Migration generated for it will not work:
$ ./manage.py makemigrations migration_test Migrations for 'migration_test': 0001_initial.py: - Create model Pointer - Create model Obj - Add field obj to pointer
$ ./manage.py migrate migration_test Operations to perform: Apply all migrations: migration_test Running migrations: Applying migration_test.0001_initial...Traceback (most recent call last): File "./manage.py", line 12, in <module> execute_from_command_line(sys.argv) File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line utility.execute() File "/usr/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute self.fetch_command(subcommand).run_from_argv(self.argv) File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv self.execute(*args, **options.__dict__) File "/usr/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute output = self.handle(*args, **options) File "/usr/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 160, in handle executor.migrate(targets, plan, fake=options.get("fake", False)) File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 63, in migrate self.apply_migration(migration, fake=fake) File "/usr/lib/python2.7/site-packages/django/db/migrations/executor.py", line 97, in apply_migration migration.apply(project_state, schema_editor) File "/usr/lib/python2.7/site-packages/django/db/backends/schema.py", line 82, in __exit__ self.execute(sql) File "/usr/lib/python2.7/site-packages/django/db/backends/schema.py", line 98, in execute cursor.execute(sql, params) File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute return super(CursorDebugWrapper, self).execute(sql, params) File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) File "/usr/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__ six.reraise(dj_exc_type, dj_exc_value, traceback) File "/usr/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute return self.cursor.execute(sql, params) django.db.utils.ProgrammingError: column "id" referenced in foreign key constraint does not exist
It happens because migrate generates SQL with wrong constraint:
$ ./manage.py sqlmigrate migration_test 0001 BEGIN; CREATE TABLE "migration_test_pointer" ("id" serial NOT NULL PRIMARY KEY, "name" varchar(10) NULL); ALTER TABLE "migration_test_pointer" ADD COLUMN "obj_id" integer NOT NULL; ALTER TABLE "migration_test_pointer" ALTER COLUMN "obj_id" DROP DEFAULT; CREATE INDEX migration_test_pointer_818ad5ef ON "migration_test_pointer" ("obj_id"); ALTER TABLE "migration_test_pointer" ADD CONSTRAINT migration_test_pointer_obj_id_7e364e33_fk_migration_test_obj_id FOREIGN KEY ("obj_id") REFERENCES "migration_test_obj" ("id") DEFERRABLE INITIALLY DEFERRED; COMMIT;
Note:
See TracTickets
for help on using tickets.
Looks like a duplicate of #23415 to me.