Search code examples
mysqldjangodjango-migrations

Django OperationalError (1142, REFERENCES command denied to user) during migration


With local MySQL was fine, but with the production is the error. After this error I see only the following tables in the database: auth_group, auth_group_permissions, auth_permission, django_content_type and django_migrations

Operations to perform:
  Synchronize unmigrated apps: djoser, messages, rest_framework, staticfiles
  Apply all migrations: account, admin, auth, authtoken, blog, commands, contenttypes, faq, servers, sessions
Synchronizing apps without migrations:
  Creating tables...
    Running deferred SQL...
Running migrations:
  Applying contenttypes.0001_initial... OK
  Applying contenttypes.0002_remove_content_type_name... OK
  Applying auth.0001_initial...Traceback (most recent call last):
  File "C:\Program Files\Python310\lib\site-packages\django\db\backends\utils.py", line 89, in _execute
    return self.cursor.execute(sql, params)
  File "C:\Program Files\Python310\lib\site-packages\django\db\backends\mysql\base.py", line 75, in execute
    return self.cursor.execute(query, args)
  File "C:\Program Files\Python310\lib\site-packages\MySQLdb\cursors.py", line 206, in execute
    res = self._query(query)
  File "C:\Program Files\Python310\lib\site-packages\MySQLdb\cursors.py", line 319, in _query
    db.query(q)
  File "C:\Program Files\Python310\lib\site-packages\MySQLdb\connections.py", line 254, in query
    _mysql.connection.query(self, query)
MySQLdb.OperationalError: (1142, "REFERENCES command denied to user 'test_user'@'22.161.40.219' for table 'django_content_type'")

Solution

  • Solution

    After the above error occurs:

    1. Run python manage.py migrate again, after which you will probably get another error like "'table_name' already exists".
    2. Run python manage.py migrate <app> --fake command for the application with which the 'table_name' table is associated.
    3. Try these 2 steps until all the necessary tables in the database are created.