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.

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?

CraigH
  • 2,041
  • 3
  • 30
  • 48
  • Take a look to my answer at http://stackoverflow.com/questions/29447863/django-mysql-error-creating-tables/37397470#37397470 Maybe someone can use my suggestion – serguitus May 23 '16 at 18:03

5 Answers5

4

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
2

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.

Arjan van Eersel
  • 119
  • 1
  • 1
  • 6
2

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

Community
  • 1
  • 1
Nick Retallack
  • 18,986
  • 17
  • 92
  • 114
  • 1
    I had the same issue when running `./manage.py migrate`. There were all MyISAM tables in my DB but default engine was set to InnoDB. Thus all new tables would be created as InnoDB what led to the `1005` error. – ta2-1 Mar 04 '17 at 14:30
0

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

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

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

CraigH
  • 2,041
  • 3
  • 30
  • 48