CraigH
CraigH

Reputation: 2061

Django Migration: Can't create table errno: 150

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.

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:

  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?

Upvotes: 10

Views: 5220

Answers (5)

Nick Retallack
Nick Retallack

Reputation: 19561

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

Upvotes: 2

Soheil Damangir
Soheil Damangir

Reputation: 41

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

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

Upvotes: 4

Arjan van Eersel
Arjan van Eersel

Reputation: 119

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

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

Upvotes: 2

CraigH
CraigH

Reputation: 2061

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

Upvotes: 0

hongshuning
hongshuning

Reputation: 77

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

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

Upvotes: 0

Related Questions