1

on running python manage.py migrate arises this error, if I delete already existing table this command still show this type of error

Apply all migrations: admin, auth, bridge, contenttypes, sessions
Running migrations:
  Applying contenttypes.0001_initial...Traceback (most recent call last):
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1050, "Table 'django_content_type' already exists")

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

Traceback (most recent call last):
  File "manage.py", line 15, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 371, in execute_from_command_line
    utility.execute()
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/core/management/__init__.py", line 365, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/core/management/base.py", line 335, in execute
    output = self.handle(*args, **options)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 200, in handle
    fake_initial=fake_initial,
  File "/usr/share/nginx/html/django_env/lib/python3.4/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 "/usr/share/nginx/html/django_env/lib/python3.4/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 "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/migrations/executor.py", line 244, in apply_migration
    state = migration.apply(state, schema_editor)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/migrations/migration.py", line 122, in apply
    operation.database_forwards(self.app_label, schema_editor, old_state, project_state)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/migrations/operations/models.py", line 92, in database_forwards
    schema_editor.create_model(model)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 314, in create_model
    self.execute(sql, params or None)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/base/schema.py", line 133, in execute
    cursor.execute(sql, params)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/utils.py", line 100, in execute
    return super().execute(sql, params)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/utils.py", line 68, in execute
    return self._execute_with_wrappers(sql, params, many=False, executor=self._execute)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/utils.py", line 77, in _execute_with_wrappers
    return executor(sql, params, many, context)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/utils.py", line 85, in _execute
    return self.cursor.execute(sql, params)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/utils.py", line 89, in __exit__
    raise dj_exc_value.with_traceback(traceback) from exc_value
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/utils.py", line 83, in _execute
    return self.cursor.execute(sql)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 71, in execute
    return self.cursor.execute(query, args)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 250, in execute
    self.errorhandler(self, exc, value)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/connections.py", line 50, in defaulterrorhandler
    raise errorvalue
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 247, in execute
    res = self._query(query)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 411, in _query
    rowcount = self._do_query(q)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/cursors.py", line 374, in _do_query
    db.query(q)
  File "/usr/share/nginx/html/django_env/lib/python3.4/site-packages/MySQLdb/connections.py", line 277, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1050, "Table 'django_content_type' already exists")
Brown Bear
  • 19,655
  • 10
  • 58
  • 76
  • Please check with django migration with fake option https://stackoverflow.com/questions/42695629/fake-initial-vs-fake-in-django-migration/42695702 – Jishnunand P K May 25 '18 at 10:06
  • I get a similar error and solve it in this [link](https://stackoverflow.com/questions/59999242/django-db-utils-internalerror-1050-table-django-content-type-already-exist/65310864#65310864). – Darwin Dec 15 '20 at 17:45

3 Answers3

1

For some reason, Django believes that the table still exists. Here's several things to do:

  1. Make sure you remove any rows that mention migrations converting this table from the migrations table in your database.
  2. Make sure that your table is actually deleted, and not simply cleared
  3. Rerun python manage.py makemigrations and python manage.py migrate.
  4. In general, since you're using Django's ORM, try not to edit or delete tables from your database manually, as this can throw Django off. Rather, delete the related model (or mark the model as an abstract one that doesn't use the database)
0

I take a look at your error log and I thought that the issue is you haven't removed migration entry from Django.migration table in the database.

-1

Windows. Download package Mysql , need version. https://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

then pip install mysql*****.whl

Kairat Koibagarov
  • 1,385
  • 15
  • 9