![](/img/trans.png)
[英]django.db.utils.OperationalError: 1005, 'Can't create table `xyz`.`#sql-600_237` (errno: 150 "Foreign key constraint is incorrectly formed")
[英]Django 1.8 app mysteriously fails with initial migration due to errno: 150 "Foreign key constraint is incorrectly formed"
我在 django 1.8 和 python 3.4 中編寫了一個應用程序,我遇到了使用 MySQL 作為數據庫后端的問題,這讓我完全被難住了。
當我從一個新數據庫開始並調用 ./manage.py migrate(或 syncdb)並嘗試創建初始數據庫時,我得到以下回溯:
(virtualenv)~/projects/projmoj (master ✘)✹✭ ᐅ ./manage.py migrate
Operations to perform:
Synchronize unmigrated apps: allauth, rest_framework, registration, rest_auth, projmoj, messages, project, staticfiles
Apply all migrations: contenttypes, sites, sessions, task, auth, admin, authtoken, static_precompiler, account
Synchronizing apps without migrations:
Creating tables...
Creating table project_project
Creating table project_membership
Running deferred SQL...
Traceback (most recent call last):
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
return self.cursor.execute(sql)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
return self.cursor.execute(query, args)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 220, in execute
self.errorhandler(self, exc, value)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorvalue
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 209, in execute
r = self._query(query)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 371, in _query
rowcount = self._do_query(q)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/cursors.py", line 335, in _do_query
db.query(q)
File "/home/tobbe/projects/projmoj/virtualenv/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
_mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1005, 'Can\'t create table `tasks`.`#sql-1c32_9` (errno: 150 "Foreign key constraint is incorrectly formed")')
當我在 web 托管服務上執行相同操作時會發生此錯誤,但它可以在托管服務和本地主機上使用 sqlite。
我沒有遷移,這些是我的模型:
class Project(models.Model):
name = models.CharField(max_length = 32)
description = models.TextField(null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
def get_owner(self):
return self.members.filter(status=OWNER).first().user
def get_membership(self, user):
return self.members.filter(user=user).first()
def __str__(self):
return str(self.name) + " - by " + str(self.get_owner().username)
class Membership(models.Model):
user = models.ForeignKey(User, related_name='joined_projects')
project = models.ForeignKey(Project, related_name='members')
status = models.PositiveSmallIntegerField(choices=MEMBER_STATUS)
join_date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.user.username)
class Sprint(models.Model):
name = models.CharField(max_length = 32, null=True, blank=True)
start_date = models.DateTimeField()
due_date = models.DateTimeField(null=True, blank=True)
project = models.ForeignKey(Project, related_name='sprints')
status = models.PositiveSmallIntegerField(choices=SPRINT_STATUS, default=PLANNING)
def __str__(self):
return str(self.name) + " - " + str(self.project)
class Category(models.Model):
name = models.CharField(max_length = 32)
project = models.ForeignKey(Project, related_name='categories')
def __str__(self):
return str(self.name) + " - " + str(self.project)
class Goal(models.Model):
name = models.CharField(max_length = 32)
description = models.TextField(null=True, blank=True)
project = models.ForeignKey(Project, related_name='goals')
completion = models.PositiveSmallIntegerField(choices=COMPLETION, default=NOT_COMPLETED)
def __str__(self):
return str(self.name) + " - " + str(self.project)
class Task(models.Model):
name = models.CharField(max_length = 64)
description = models.TextField(null=True, blank=True)
project = models.ForeignKey(Project, related_name='tasks')
category = models.ForeignKey(Category, related_name='tasks', null=True, blank=True)
goal = models.ForeignKey(Goal, related_name='tasks', null=True, blank=True)
creation_date = models.DateTimeField(auto_now_add=True)
sprint = models.ForeignKey(Sprint, related_name='tasks', null=True, blank=True)
status = models.PositiveSmallIntegerField(choices=TASK_STATUS, default=WAITING)
dedicated_hours = models.DecimalField(max_digits=6, decimal_places=2, null=True, blank=True)
assigned_worker = models.ForeignKey(User, null=True, blank=True)
priority = models.PositiveSmallIntegerField(choices=PRIORITY, default=NORMAL)
def __str__(self):
return str(self.name) + " - " + str(self.project)
class HourReport(models.Model):
task = models.ForeignKey(Task, related_name='hour_reports')
worker = models.ForeignKey(User, related_name='hour_reports')
hours_spent = models.DecimalField(max_digits=6, decimal_places=2)
date = models.DateTimeField(auto_now_add=True)
def __str__(self):
return str(self.hours_spent) + " on " + str(self.task)
我的數據庫設置:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.mysql',
'NAME': 'tasks',
'USER': 'root',
'PASSWORD': 'mysqlmysql',
'HOST': 'localhost',
}
}
版本等:
谷歌搜索時,這個錯誤似乎經常出現在人們填充他們的手動 SQL 代碼時,但我通過 django 自動完成。
有任何想法嗎?
日志告訴您尚未為project
應用程序創建遷移。 您應該在運行遷移之前為應用程序創建遷移。
./manage.py makemigrations project
./manage.py migrate
由於它是一個新數據庫,您最好在創建和應用遷移之前完全重置它,否則您將不得不手動處理其當前不一致的狀態。
如果您將現有項目升級到
MySQL 5.5.5
並隨后添加一些表,請確保您的表使用相同的存儲引擎(即MyISAM
與InnoDB
)。 具體來說,如果表之間有一個ForeignKey
使用不同的存儲引擎,您可能會在運行migrate
時看到如下錯誤:_mysql_exceptions.OperationalError: ( 1005, "Can't create table '\\\\db_name\\\\.#sql-4a8_ab' (errno: 150)" )
停止您的后端並進行備份。 將表的存儲引擎從 MyISAM 更改為 InnoDB。 這是解決問題。
同樣在 MySQL 文檔( B.3 服務器錯誤代碼和消息章節)中指定了關於此錯誤:
錯誤:1005 SQLSTATE: HY000 (ER_CANT_CREATE_TABLE)
消息:無法創建表“%s”(錯誤號:%d - %s)
當無法創建表時,InnoDB 會報告此錯誤。 如果錯誤消息涉及錯誤 150,則表創建失敗,因為外鍵約束未正確形成。 如果錯誤消息指向錯誤 -1,則表創建可能失敗,因為該表包含與內部 InnoDB 表名稱匹配的列名稱。
運行 python manage.py migrate
時也有可能得到該錯誤
_mysql.connection.query(自我,查詢)
django.db.utils.OperationalError: (1005, 'Can\\'t create table >
codelabs
django_admin_log
(errno: 150 "外鍵約束形成不正確")')
由於 settings.py 中關於 DATABASES 的信息有誤。 對於情況下,如果在你的數據庫表中有名字蒂亞戈p ERES並在settings.py你闖民宅的名字蒂亞戈P ERES,該錯誤會顯示出來。 要修復它,只需使用正確的 NAME 並再次運行python manage.py migrate
。
我今天也遇到了這個問題並閱讀了沒有解決問題的文章和 StackOverflow。 然后我刪除了我的數據庫,因為沒有什么特別的,並刪除了所有遷移文件並再次開始遷移過程。 問題解決了。
查看 MySql 或 MariaDB 控制台客戶端中命令SHOW ENGINE INNODB STATUS
的 output( mariadb 鏈接, mysql 鏈接)。 它的信息量更大。 例如,它向我展示了這條消息:
...
------------------------
LATEST FOREIGN KEY ERROR
------------------------
2021-09-20 18:27:08 7faea3ad1700 Error in foreign key constraint of table `my_db`.`django_admin_log`:
Alter table `my_db`.`django_admin_log` with foreign key constraint failed. Referenced table `my_db`.`profiles_userprofile` not found in the data dictionary near ' FOREIGN KEY (`user_id`) REFERENCES `profiles_userprofile` (`id`)'.
...
它對我說我忘了創建遷移。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.