Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 567
CraigH Avatar asked Jun 28 '15 13:06

CraigH


Video Answer


3 Answers

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
like image 92
Soheil Damangir Avatar answered Oct 11 '22 14:10

Soheil Damangir


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.

like image 2
Arjan van Eersel Avatar answered Oct 11 '22 14:10

Arjan van Eersel


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

like image 2
Nick Retallack Avatar answered Oct 11 '22 14:10

Nick Retallack