简体   繁体   中英

How can I resolve 'django_content_type already exists'?

After upgrading to django 1.8 I'm recieving the error during migration:

ProgrammingError: relation "django_content_type" already exists

I'd be interested in the background behind this error, but more importantly, How can I resolve it?

Initial migrations on a project can sometimes be troubleshot using --fake-initial

python manage.py migrate --fake-initial

It's new in 1.8. In 1.7, --fake-initial was an implicit default, but explicit in 1.8.

From the Docs:

The --fake-initial option can be used to allow Django to skip an app's initial migration if all database tables with the names of all models created by all CreateModel operations in that migration already exist. This option is intended for use when first running migrations against a database that preexisted the use of migrations. This option does not, however, check for matching database schema beyond matching table names and so is only safe to use if you are confident that your existing schema matches what is recorded in your initial migration.

https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---fake-initial

I solved this issue on Django 2.2.7 or Django 3.0 hosted on Ubuntu 18.04 + Postgres 10.10 version.

  1. Restore the database in Postgres database (used pgAdmin tool for this)
  2. (virtualenv)python manage.py loaddata dumpfile.json
  3. Dropping django_migrations table from database (used pgAdmin tool for this)
  4. (virtualenv)python manage.py makemigrations
  5. (virtualenv)python manage.py migrate --fake
  6. (virtualenv)python manage.py migrate
  7. (virtualenv)python manage.py collectstatic
  8. (virtualenv)python manage.py runserver 0.0.0.0:8000

我授予用户对该特定数据库的所有权限,并解决了该问题。

Hi this did not work for my app hosted in Django 2.2.7 version. Here is the exception. Please let me know of someone has a clue on this?

    $ ~/vkproject/manage.py migrate --fake-initial
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pages, sessions
Running migrations:
  Applying contenttypes.0001_initial... FAKED
  Applying auth.0001_initial... FAKED
  Applying admin.0001_initial... FAKED
  Applying admin.0002_logentry_remove_auto_add... OK
  Applying admin.0003_logentry_add_action_flag_choices... OK
  Applying contenttypes.0002_remove_content_type_name...Traceback (most recent call last):
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UndefinedColumn: column "name" of relation "django_content_type" does not exist


The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/home/shami/vkproject/manage.py", line 21, in <module>
    main()
  File "/home/shami/vkproject/manage.py", line 17, in main
    execute_from_command_line(sys.argv)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/base.py", line 83, in wrapped
    res = handle_func(*args, **kwargs)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/core/management/commands/migrate.py", line 234, in handle
    fake_initial=fake_initial,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 117, in migrate
    state = self._migrate_all_forwards(state, plan, full_plan, fake=fake, fake_initial=fake_initial)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 147, in _migrate_all_forwards
    state = self.apply_migration(state, migration, fake=fake, fake_initial=fake_initial)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/executor.py", line 245, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/migration.py", line 124, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/migrations/operations/fields.py", line 249, in database_forwards
    schema_editor.alter_field(from_model, from_field, to_field)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 535, in alter_field
    old_db_params, new_db_params, strict)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/postgresql/schema.py", line 124, in _alter_field
    new_db_params, strict,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 685, in _alter_field
    params,
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 137, in execute
    cursor.execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 99, in execute
    return super().execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 67, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 76, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 84, in _execute
    return self.cursor.execute(sql, params)
django.db.utils.ProgrammingError: column "name" of relation "django_content_type" does not exist

Thanks in advance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM