django.db.utils.ProgrammingError: relation "django_content_type" already exists

5,659 views
Skip to first unread message

Ram

unread,
Nov 30, 2019, 12:04:04 PM11/30/19
to django...@googlegroups.com
 Hi,

I'm blocked with this issue in my newly hosting site. Here is my scenario.

  1. I've working site with MySQL database in our development localhost.
  2. I created new hosting server in Digital Ocean's Ubuntu 18.04 droplet with Postgres db.
  3. I tested the server with empty database and empty Django site when I build this hosting server.
  4. I manually migrated MySQL database to Postgres using pgAdmin tool and ran 'loaddata'command in virtualenv to load the data to Postgress.
  5. Now when I ran the following commands I see the same issue as reported here.

  6. (vkenv) shami@ubuntu-wed-01:~$ ~/vkproject/manage.py makemigrations
    No changes detected
    (vkenv) shami@ubuntu-wed-01~$ ~/vkproject/manage.py migrate
    Operations to perform:
      Apply all migrations: admin, auth, contenttypes, pages, sessions
    Running migrations:
      Applying contenttypes.0001_initial...Traceback (most recent call last):
      File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/utils.py", line 82, in _execute
        return self.cursor.execute(sql)
    psycopg2.errors.DuplicateTable: relation "django_content_type" already exists

    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/models.py", line 92, in database_forwards
        schema_editor.create_model(model)
      File "/home/shami/vkproject/vkenv/lib/python3.6/site-packages/django/db/backends/base/schema.py", line 307, in create_model
        self.execute(sql, params or None)
      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 82, in _execute
        return self.cursor.execute(sql)
    django.db.utils.ProgrammingError: relation "django_content_type" already exists

  7.     (vkenv) shami@ubuntu-wed-01:~$ ~/vkproject/manage.py showmigrations
        admin
         [ ] 0001_initial
         [ ] 0002_logentry_remove_auto_add
         [ ] 0003_logentry_add_action_flag_choices
        auth
         [ ] 0001_initial
         [ ] 0002_alter_permission_name_max_length
         [ ] 0003_alter_user_email_max_length
         [ ] 0004_alter_user_username_opts
         [ ] 0005_alter_user_last_login_null
         [ ] 0006_require_contenttypes_0002
         [ ] 0007_alter_validators_add_error_messages
         [ ] 0008_alter_user_username_max_length
         [ ] 0009_alter_user_last_name_max_length
         [ ] 0010_alter_group_name_max_length
         [ ] 0011_update_proxy_permissions
        contenttypes
         [ ] 0001_initial
         [ ] 0002_remove_content_type_name
        pages
         [ ] 0001_initial
         [ ] 0002_auto_20190504_1638
         [ ] 0003_auto_20190504_1645
         [ ] 0004_auto_20190504_1651
         [ ] 0005_auto_20190506_1451
         [ ] 0006_auto_20190506_1558
         [ ] 0007_auto_20190506_1803
         [ ] 0008_auto_20190508_1455
         [ ] 0009_auto_20190508_1554
         [ ] 0010_rim_banners
         [ ] 0011_rim_banners_link_id
         [ ] 0012_auto_20190520_1137
         [ ] 0013_auto_20190520_1144
         [ ] 0014_rim_transactions_cj
         [ ] 0015_auto_20190617_1756
         [ ] 0016_auto_20190619_1433
         [ ] 0017_rim_cj_feed
        sessions
         [ ] 0001_initial
        (vkenv) shami@ubuntu-wed-01:~$ 
            
        (vkenv) shami@ubuntu-wed-01:~$ ~/vkproject/manage.py --version
        2.2.7

I appreciate if someone can provide a clue here.

Thanks in advance,

~Ram

Integr@te System

unread,
Nov 30, 2019, 1:29:06 PM11/30/19
to django...@googlegroups.com
Hi issuer,

You do step 4 and step 5 with the same goal!?

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CA%2BOi5F2GuoQAaiDY_cWzemddpKhrjwWixJMmdyrDRJoT98zs_w%40mail.gmail.com.

Ram

unread,
Nov 30, 2019, 2:14:19 PM11/30/19
to django...@googlegroups.com
Hi Integr,

Thank you for your email.

Your mean these steps have to be run again?

4. 'loaddata'command in virtualenv to load the data to Postgress.
5. ~/vkproject/manage.py makemigrations
6. ~/vkproject/manage.py migrate

~Ram

Ram

unread,
Dec 4, 2019, 1:19:16 AM12/4/19
to django...@googlegroups.com
Hi Integr,

I tried as per your suggestion and the issue is not fix yet. Please let me know what I'm missing? Here is the result of each command.

(vkenv) shami@ubuntu-wed-01:~/vkproject$ python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > vikreya120319afterdroppingcron.json
(vkenv) shami@ubuntu-wed-01:~/vkproject$

(vkenv) shami@ubuntu-wed-01:~/vkproject$ python manage.py loaddata vikreya120319afterdroppingcron.json
Installed 82150 object(s) from 1 fixture(s)
(vkenv) shami@ubuntu-wed-01:~/vkproject$

(vkenv) shami@ubuntu-wed-01:~/vkproject$ python manage.py makemigrations
No changes detected
(vkenv) shami@ubuntu-wed-01:~/vkproject$

(vkenv) shami@ubuntu-wed-01:~/vkproject$ python manage.py migrate

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pages, sessions
Running migrations:
  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 "manage.py", line 21, in <module>
    main()
  File "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

(vkenv) shami@ubuntu-wed-01:~/vkproject$


Best regards,
~Ram

Naveen

unread,
Dec 4, 2019, 6:28:08 AM12/4/19
to Django users

Ram

unread,
Dec 4, 2019, 5:42:18 PM12/4/19
to django...@googlegroups.com
Hello Naveen,

I already tried that before posting my last update but the result is the same though. Here I'm pasting the result. This is a total blocker to me. I appreciate if someone can unblock me.

$ python manage.py migrate --fake-initial

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, pages, sessions
Running migrations:
  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 "manage.py", line 21, in <module>
    main()
  File "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
--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.

Ram

unread,
Dec 7, 2019, 7:09:01 PM12/7/19
to django...@googlegroups.com
Hi,

As a next step I tried the following but the result is is same. Please let me know if you have any clue on this?

1. Deleted/Dropped 'django_content_type' table from database
2. I upgraded Django to version 3.0 from 2.2.7 in my virtual environment and ran the following commands

1. $ python manage.py migrate --fake
2. $ python manage.py migrate --fake--initial
3. $ python manage.py migrate

But I still see same error

django.db.utils.ProgrammingError: relation "django_content_type" does not exist
LINE 1: ..."."app_label", "django_content_type"."model" FROM "django_co...

Integr@te System

unread,
Dec 8, 2019, 9:29:46 AM12/8/19
to django...@googlegroups.com
Hi issuer,

Check if your app development in localhost whether fall into any restricted case relate to Django content type:



Ram

unread,
Dec 8, 2019, 10:09:05 AM12/8/19
to django...@googlegroups.com
Thank you for the pointer. I've solved this issue few minutes back as follows.

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

My server is up and running now after struggling for a week!

Best regards


Reply all
Reply to author
Forward
0 new messages