#23479: makemigrations geterates wrong migration for ForeignKey to unmanaged 
model
-------------------------------+--------------------
     Reporter:  g1itch         |      Owner:  nobody
         Type:  Uncategorized  |     Status:  new
    Component:  Migrations     |    Version:  1.7
     Severity:  Normal         |   Keywords:
 Triage Stage:  Unreviewed     |  Has patch:  0
Easy pickings:  0              |      UI/UX:  0
-------------------------------+--------------------
 I wrote a simple test app:
 {{{#!python
 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:
 {{{#!sh
 $ ./manage.py makemigrations migration_test
 Migrations for 'migration_test':
   0001_initial.py:
     - Create model Pointer
     - Create model Obj
     - Add field obj to pointer
 }}}

 {{{#!sh
 $ ./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:

 {{{#!sh
 $ ./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;
 }}}

--
Ticket URL: <https://code.djangoproject.com/ticket/23479>
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/049.dc13dd70b7af7ad38f577f58eec8300b%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to