Django Migration:无法创建表错误:150

时间:2015-06-28 13:43:54

标签: django django-models

我有一个全新的MariaDB服务(版本:5.5.41-MariaDB)并为我的Django(1.8.2)应用程序创建了一个新数据库。默认情况下,使用innoDB创建数据库。

我有一个模型,如下所示:

class UserProfile(models.Model):
    user = models.OneToOneField(User, unique=True)  # django's default user model

当我运行python manage.py 迁移时。我收到以下错误:

  File "/home/vagrant/envs/leo/lib/python2.7/site-packages/MySQLdb/cursors.py", line 205, in execute
    self.errorhandler(self, exc, value)
  File "/home/vagrant/envs/leo/lib/python2.7/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorclass, errorvalue
django.db.utils.OperationalError: (1005, "Can't create table 'leo.#sql-bcd_1f' (errno: 150)").

我做错了什么,如何解决?

5 个答案:

答案 0 :(得分:4)

如果不创建迁移文件,也会发生此错误。确保在实际迁移之前运行makemigrations

python manage.py makemigrations <app_name>
python manage.py migrate

答案 1 :(得分:2)

我在django 1.8下遇到了同样的问题,唯一的区别是我使用的是MySQL而不是MariaDB。

将数据库的编码从utf8_unicode_ci更改为utf_general_ci为我解决了这个问题。

答案 2 :(得分:2)

这个奇怪的MySQL错误是gulp-load-plugins not loading plugins。我不确定你的Django应用程序是如何触发它的。

答案 3 :(得分:0)

OneToOneField不需要唯一属性,就像这样:

class UserProfile(models.Model):
    user = models.OneToOneField(User)

答案 4 :(得分:0)

我设法通过降级到Django 1.7.9来解决这个问题。降级后它立即起作用