Error creating new content types. Please make sure contenttypes is migrated before trying to migrate apps individually
Asked Answered
S

6

35

I am trying to migrate from Django 1.6 to Django 1.8. I was using South for managing migrations in Django 1.6. I have successfully created new migration files by python manage.py makemigrations. while running python manage.py migrate --fake-initial, I am getting this error

 Traceback (most recent call last):
  File "manage.py", line 39, in <module>
    execute_from_command_line(sys.argv)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-    packages/django/core/management/__init__.py", line 338, in   execute_from_command_line
    utility.execute()
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-  packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/base.py", line 390, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/base.py", line 441, in execute
    output = self.handle(*args, **options)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 225, in handle
    emit_post_migrate_signal(created_models, self.verbosity, self.interactive, connection.alias)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/core/management/sql.py", line 280, in emit_post_migrate_signal
using=db)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/dispatch/dispatcher.py", line 201, in send
response = receiver(signal=self, sender=sender, **named)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/contrib/auth/management/__init__.py", line 82, in create_permissions
    ctype = ContentType.objects.db_manager(using).get_for_model(klass)
  File "/home/jonty/.virtualenvs/squadrun/local/lib/python2.7/site-packages/django/contrib/contenttypes/models.py", line 78, in get_for_model
    "Error creating new content types. Please make sure contenttypes "

One of the migration file 0001_initial.py says:

dependencies = [
    ('auth', '0006_require_contenttypes_0002'),
    ('clients', '0002_auto_20150428_1551'),
    ('players', '0001_initial'),
]

which I guess is particularly the problem. What could be the workaround this problem. Any help will be appreciated.

Storekeeper answered 28/4, 2015 at 10:54 Comment(3)
The dependency on auth 0006 should ensure that contenttypes is migrated before the post migration signal is run. What's the error if you manually run ContentType.objects.get(app_label=<app label>, model_name=<model name>)?Deus
ContentType matching query does not existStorekeeper
And with get_or_create()?Deus
Z
48

I think this has something to do with "The removal of ContentType.name", according to this. But somehow it doesnt work.

By manually removing the column name from 'django_content_type' table. Eg.

'ALTER TABLE django_content_type DROP COLUMN name'

I was able to apply the migrations. Maybe this can get you a little bit further at least.

Zweig answered 5/5, 2015 at 18:10 Comment(4)
I was able to make this work by completly deleting the table django_content_type and running the migration. Yeah and it's because the name has been removed in Django 1.8.Storekeeper
Another option, create a temporary database, do a syncdb etc., then copy the django_content_type table into the problem database.Rickettsia
There is django/contrib/contenttypes/migrations/0002_remove_content_type_name.py in Django 1.8. Didn't check when it was added.Froe
@Storekeeper Interestingly, I dropped the table as well and yet the error persists. :/Mythos
K
18

Try to migrate auth application first, and then others:

manage.py migrate auth
manage.py migrate <app_name>
Kavita answered 16/3, 2016 at 8:53 Comment(1)
@Stryker I'm so glad to hear that :)Kavita
V
7

In my case, what i did to fix this was updating to a newer version of django. If you work with mac just do:

  1. pip install django --upgrade
  2. python manage.py makemigrations
  3. python manage.py migrate
Vanpelt answered 5/9, 2015 at 23:27 Comment(0)
P
3

May look strange but I fixed this by upgrading to Django version 1.8. Initially i was using ver 1.7

Pleven answered 15/5, 2015 at 7:18 Comment(1)
Upgrading to a newer version did solve my problem. Thanks!Hoax
H
2

To add to comment by @int_ua Add this as a dependency to the migration that is failing:

dependencies = [
    ('contenttypes', '0002_remove_content_type_name'),
]

Then run migration again.

Heavyarmed answered 17/3, 2016 at 8:59 Comment(0)
G
2

I had to merge two systems in Django 1.9.1 and I just could not get past this error:

 "Error creating new content types. Please make sure contenttypes "

Extensive googling and stackoverflowing was fruitless. Finally, I added the the debug line to

~/.virtualenvs/(venv_name)/lib/python2.7/site-packages/django/contrib/contenttypes/models.py

 except (OperationalError, ProgrammingError, IntegrityError):
        # It's possible to migrate a single app before contenttypes,
        # as it's not a required initial dependency (it's contrib!)
        # Have a nice error for this.
        print "\n\nError for Content type model "+opts.model_name+"\n\n"
        raise RuntimeError(
            "Error creating new content types. Please make sure contenttypes "
            "is migrated before trying to migrate apps individually."
        )

This told me the model names that were causing the error and ultimately led to the fix.

I am using Postgres and the sequence numbers for tables django_content_type and auth_permission were not pointing to the end of the table, causing inserts to fail.

These 2 lines fixed that (based on this SO post)

SELECT pg_catalog.setval(pg_get_serial_sequence('django_content_type', 'id'), (SELECT MAX(id) FROM django_content_type)+1);
SELECT pg_catalog.setval(pg_get_serial_sequence('auth_permission', 'id'), (SELECT MAX(id) FROM auth_permission)+1);
Ganoid answered 27/2, 2017 at 5:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.