我正在使用python 3.4在django 1.8中编写应用程序,我遇到了使用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托管服务上执行相同操作时会发生此错误,但是它可以在托管服务和localhost上使用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',
}
}
版本等:
> Django 1.8
> Python 3.4
>默认存储引擎:InnoDB
> Mysqlclient 1.3.6
>服务器:Amazon RDS上的MySQL 5.5.42(在虚拟主机上)
>服务器:10.0.21-MariaDB-log MariaDB服务器(localhost)
当谷歌搜索时,似乎这个错误通常是人们填写他们的手动SQL代码,但我通过django自动完成.
有任何想法吗?