3

I am trying to unitest my models in my django application that consisits of some migrations. I have prepared my test, but when running

./manage.py test my_app

i get the following error

django.db.utils.OperationalError: (1005, "Can't create table 'test_rhombusdb.#sql-4ca_2c' (errno: 150)")

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?

what other info would you like?

my stack trace

 File "./manage.py", line 18, in <module>
    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)")

I can see there is an error when migration is called.

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

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

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.

class Patient(models.Model):
    #many fields here

   doctor = models.ForeignKey(settings.AUTH_USER_MODEL, related_name="patients")

It worked with 1.6 and sqlite3, I moved to 1.8 and mysql, and boom...error.

AUTH_USER_MODEL = 'users.MyCustomUser'

MyCustomUser is a class inheriting AbstractBaseUser and PermissionsMixin.

11
  • Try to delete all tables (testing db) and then re-run the tests again, see this: code.djangoproject.com/ticket/18256, also it could be an order migrations issue. Commented Sep 12, 2015 at 20:12
  • 1
    I am using django 1.8.x. I am using MySql and no testing db exists. When it throws the exception testing_db won't get dropped so it stays on my server. If I rerun the test, it asks me to delete it. Deleting it will cause the same issue, probably because it can't create the database. I think the error refers to syncdb in your link while mine happens during test. When I run migrate (syncdb equivalent) everything works fine. I have migrated from 1.6 to 1.8 and created migrations using --fake-inital flag Commented Sep 12, 2015 at 20:22
  • Glad it works, yeah using migrate command is the right way now. Commented Sep 12, 2015 at 20:34
  • It doesn't work...My problems is when testing ./migrate test gives me the error Commented Sep 12, 2015 at 20:45
  • Sorry I had understood wrong, so has your db user the right permissions (creating, deleting dbs)? Commented Sep 12, 2015 at 21:32

1 Answer 1

1

I had the same (pretty general) error message. For me it came down to having forgot to run makemigrations on a new app. See also:

https://docs.djangoproject.com/en/1.8/topics/migrations/#dependencies

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.