title: Unit testing django application error creating database table OperationalError: (1005, "Can't create table...)
body: <p>I am trying to unitest my models in my django application that consisits of some migrations. I have prepared my test, but when running </p>
  
  <pre><code>./manage.py test my_app
  </code></pre>
  
  <p>i get the following error</p>
  
  <pre><code>django.db.utils.OperationalError: (1005, "Can't create table 'test_rhombusdb.#sql-4ca_2c' (errno: 150)")
  </code></pre>
  
  <p>The database is created normally, So I guess I have permissions to create the db. 
  I really don't know which table this is reffering to. Migrations pass normal and db works fine in production. What could be the issue here?</p>
  
  <p>what other info would you like?</p>
  
  <p>my stack trace</p>
  
  <pre><code> File "./manage.py", line 18, in &lt;module&gt;
      execute_from_command_line(sys.argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
      utility.execute()
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 330, in execute
  self.fetch_command(subcommand).run_from_argv(self.argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 30, in run_from_argv
     super(Command, self).run_from_argv(argv)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 393, in run_from_argv
      self.execute(*args, **cmd_options)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 74, in execute
      super(Command, self).execute(*args, **options)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
      output = self.handle(*args, **options)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/test.py", line 90, in handle
      failures = test_runner.run_tests(test_labels)
    File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 210, in run_tests
      old_config = self.setup_databases()
    File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 166, in setup_databases
      **kwargs
    File "/usr/local/lib/python2.7/dist-packages/django/test/runner.py", line 370, in setup_databases
      serialize=connection.settings_dict.get("TEST", {}).get("SERIALIZE", True),
    File "/usr/local/lib/python2.7/dist-packages/django/db/backends/base/creation.py", line 368, in create_test_db
      test_flush=not keepdb,
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/__init__.py", line 120, in call_command
      return command.execute(*args, **defaults)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/base.py", line 444, in execute
      output = self.handle(*args, **options)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 179, in handle
      created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
    File "/usr/local/lib/python2.7/dist-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
      cursor.execute(statement)
    File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 64, in execute
      return self.cursor.execute(sql, params)
    File "/usr/local/lib/python2.7/dist-packages/django/db/utils.py", line 97, in __exit__
      six.reraise(dj_exc_type, dj_exc_value, traceback)
    File "/usr/local/lib/python2.7/dist-packages/django/db/backends/utils.py", line 62, in execute
      return self.cursor.execute(sql)
    File "/usr/local/lib/python2.7/dist-packages/django/db/backends/mysql/base.py", line 124, in execute
      return self.cursor.execute(query, args)
    File "/usr/local/lib/python2.7/dist-packages/MySQLdb/cursors.py", line 205, in execute
      self.errorhandler(self, exc, value)
    File "/usr/local/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
      raise errorclass, errorvalue
  django.db.utils.OperationalError: (1005, "Can't create table 'testdb.#sql-4ca_7c' (errno: 150)")
  </code></pre>
  
  <p>I can see there is an error when migration is called.</p>
  
  <p>PS: I droped my database(real one but the developement one) and tried to recreate tables running migrate. I came with the same error. Running migrate again worked like a charm....Something is really really really wrong here.... </p>
  
  <p>PS2: Deleting all migration folders and starting without gave me the same error after running migrate with no migration folders present....Very bad error very difficult to debug</p>
  
  <p>PS3: There is an issue with the foreign key on one (at least so far i've tested) of my models. I tried deactivating apps and activating them in terms, and when I got to a specific one, error occured.I isolated one model, and gave me the issue. I commented out the foreign keys and worked.  Uncomment one of them error again. </p>
  
  <pre><code>class Patient(models.Model):
      #many fields here
  
     doctor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="patients")
  </code></pre>
  
  <p>It worked with 1.6 and sqlite3, I moved to 1.8 and mysql, and boom...error. </p>
  
  <pre><code>AUTH_USER_MODEL = 'users.MyCustomUser'
  </code></pre>
  
  <p>MyCustomUser is a class inheriting AbstractBaseUser and PermissionsMixin. </p>