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

时间:2022-04-27 20:44:10

I have a brand new MariaDB serve (version: 5.5.41-MariaDB) and created a new database for my Django (1.8.2) application. The database was created using innoDB by default.

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

I have a model that looks like this:

我有一个看起来像这样的模型:

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

When I run python manage.py migrate. I get the following error:

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

  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)").

What am I doing wrong and how can I fix it?

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

5 个解决方案

#1


3  

This error can also occur when you don't make the migration files. Be sure to run makemigrations before actual migration.

不进行迁移文件时也会发生此错误。确保在实际迁移之前运行makemigrations。

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

#2


2  

I had the same problem under django 1.8, the only difference is that I'm using MySQL and not MariaDB.

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

Changing the database's encoding from utf8_unicode_ci to utf_general_ci solved the problem for me.

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

#3


2  

This bizarre MySQL error is explained here. I'm not sure how your Django app is triggering it though.

这里解释了这个奇怪的MySQL错误。我不确定你的Django应用程序是如何触发它的。

#4


0  

OneToOneField doesn't need unique attribute, just like this:

OneToOneField不需要唯一属性,如下所示:

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

#5


0  

I managed to resolve this by downgrading to Django 1.7.9. It worked straight away after downgrading

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

#1


3  

This error can also occur when you don't make the migration files. Be sure to run makemigrations before actual migration.

不进行迁移文件时也会发生此错误。确保在实际迁移之前运行makemigrations。

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

#2


2  

I had the same problem under django 1.8, the only difference is that I'm using MySQL and not MariaDB.

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

Changing the database's encoding from utf8_unicode_ci to utf_general_ci solved the problem for me.

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

#3


2  

This bizarre MySQL error is explained here. I'm not sure how your Django app is triggering it though.

这里解释了这个奇怪的MySQL错误。我不确定你的Django应用程序是如何触发它的。

#4


0  

OneToOneField doesn't need unique attribute, just like this:

OneToOneField不需要唯一属性,如下所示:

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

#5


0  

I managed to resolve this by downgrading to Django 1.7.9. It worked straight away after downgrading

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