Opened 8 years ago

Closed 8 years ago

Last modified 8 years ago

#22881 closed Bug (fixed)

initial migration fails just after switching to django-1.7(beta)

Reported by: Thomas Capricelli Owned by: nobody
Component: Migrations Version: 1.7-beta-2
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


I'm currently testing 1.7beta.

I've had the problem for several days. Currently using django 1.7 branch of git as of today.

The code for my application is using django-1.6 but is almost ready for 1.7. The only remaining change is the deletion of admin.autodiscover() in (can't do that in 1.6).

The db has "never seen" django 1.7 before. I'm starting my test from a copy of the production db (mysql) imported in sqlite using djang 1.6.

I then switch to django 1.7 and does this :

% ./ check
System check identified no issues (0 silenced).

% ./ migrate
Operations to perform:

Synchronize unmigrated apps: main, registration
Apply all migrations: sessions, admin, sites, flatpages, contenttypes, auth

Synchronizing apps without migrations:

Creating tables...
Installing custom SQL...
Installing indexes...

Running migrations:

Applying contenttypes.0001_initial...Traceback (most recent call last):
File "./", line 7, in <module>


File "/usr/lib64/python2.7/site-packages/django/core/management/", line 385, in execute_from_command_line


File "/usr/lib64/python2.7/site-packages/django/core/management/", line 377, in execute


File "/usr/lib64/python2.7/site-packages/django/core/management/", line 288, in run_from_argv

self.execute(*args, options.dict)

File "/usr/lib64/python2.7/site-packages/django/core/management/", line 337, in execute

output = self.handle(*args, options)

File "/usr/lib64/python2.7/site-packages/django/core/management/commands/", line 148, in handle

executor.migrate(targets, plan, fake=options.get("fake", False))

File "/usr/lib64/python2.7/site-packages/django/db/migrations/", line 62, in migrate

self.apply_migration(migration, fake=fake)

File "/usr/lib64/python2.7/site-packages/django/db/migrations/", line 96, in apply_migration

migration.apply(project_state, schema_editor)

File "/usr/lib64/python2.7/site-packages/django/db/migrations/", line 107, in apply

operation.database_forwards(self.app_label, schema_editor, project_state, new_state)

File "/usr/lib64/python2.7/site-packages/django/db/migrations/operations/", line 36, in database_forwards


File "/usr/lib64/python2.7/site-packages/django/db/backends/", line 270, in create_model

self.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/backends/", line 98, in execute

cursor.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/backends/", line 81, in execute

return super(CursorDebugWrapper, self).execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/backends/", line 65, in execute

return self.cursor.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/", line 94, in exit

six.reraise(dj_exc_type, dj_exc_value, traceback)

File "/usr/lib64/python2.7/site-packages/django/db/backends/", line 65, in execute

return self.cursor.execute(sql, params)

File "/usr/lib64/python2.7/site-packages/django/db/backends/sqlite3/", line 485, in execute

return Database.Cursor.execute(self, query, params)

django.db.utils.OperationalError: table "django_content_type" already exists

I can turn around this using --fake.

I'm not totally sure it's exactly the same problem, but this second one does look very close :

The "main" application you see upthere is so-called 'unmigrated'. Following the doc, i migrate using "makemigrations", and then migrate again.

I got the same django.db.utils.OperationalError table already exists on the first model created by this migration.

My reference is :

that says :
"This will make a new initial migration for your app. Now, when you run migrate, Django will detect that you have an initial migration and that the tables it wants to create already exist, and will mark the migration as already applied."

Change History (8)

comment:1 Changed 8 years ago by Chris Beaven

Error is due to test_soft_apply failing if any non-CreateModel migrations exist in the initial migration. This is the case for contenttypes (and since that's so simple, surely many other initial migrations too).

comment:2 Changed 8 years ago by Thomas Capricelli

i confirm that it is also the case in the migration created by makemigration. For some really weird reasons i dont understand, instead of creating normal "ForeignKey" it first creates the model without it, then does


This model is called "Candidat", and is defined last in my (because it has ForeignKey's on model defined before, i know i could use strings but i dont here). In the migration file, the order is

  • create "Candidat"
  • create another one
  • migrations.AddField to add a key in "Candidat" toward this second model.

"Candidat" also has a ForeignKey to "User" but this one is defined normally in the migration file.

That looks like :

operations = [



('user', models.ForeignKey(to=settings.AUTH_USER_MODEL, to_field='id', unique=True)),





field=models.ForeignKey(to='main.Other', to_field='id'),


comment:3 Changed 8 years ago by Chris Beaven <smileychris@…>

Resolution: fixed
Status: newclosed

In 21c496ea52b8bdf0d97507c00a87286425dac087:

Fixed #22881 -- Better soft_applied migration detection

comment:4 Changed 8 years ago by Chris Beaven <smileychris@…>

Resolution: fixed
Status: newclosed

In 29582ad4a3ef5888ef86a45167ed8e7414c1347e:

[1.7.x] Fixed #22881 -- Better soft_applied migration detection

comment:6 Changed 8 years ago by Thomas Capricelli

I tried with an updated django and it fixes both my problems indeed..
Thanks !

(I'm no authoritative, but the ci error doesn't seem related)

comment:7 Changed 8 years ago by Tim Graham <timograham@…>

In 908160f6926506dc64244e9d5a4507bc67ea0a81:

Fixed test failures introduced by refs #22881.

comment:8 Changed 8 years ago by Tim Graham <timograham@…>

In 21c959138401bd9f57fd789409c80add01c4b075:

[1.7.x] Fixed test failures introduced by refs #22881.

Backport of 908160f692 from master

Note: See TracTickets for help on using tickets.
Back to Top