本地 MySQL 没问题,但生产环境就报错。出现此错误后,我在数据库中仅看到以下表格:
auth_group
、auth_group_permissions
、auth_permission
、django_content_type
和 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'")
解决方案
出现上述错误后:
python manage.py migrate
,之后您可能会收到另一个错误,例如"'table_name' already exists"
。python manage.py migrate <app> --fake
表关联的应用程序运行 'table_name'
命令。我知道这是一个旧线程,但我最近遇到了非常相同的问题,而解决方案是我忽略的一件愚蠢的事情。
虽然接受的解决方案确实有效,但如果您有很多应用程序并且不知道错误源自何处,最终可能会让您感到沮丧。
这是我为用于 mysql 连接的用户设置的权限
GRANT SELECT,INSERT,CREATE,ALTER,DROP,LOCK TABLES,CREATE TEMPORARY TABLES, DELETE,UPDATE,EXECUTE ON customdb.* TO 'user'@'%';
当时我没有意识到
REFERENCES
是要设置的权限。
我跑了
GRANT REFERENCES ON customdb.* tp 'user'@'%';
在我的 MySQL 和瞧,问题解决了。