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.

Apostolos
  • 7,763
  • 17
  • 80
  • 150
  • Try to delete all tables (testing db) and then re-run the tests again, see this: https://code.djangoproject.com/ticket/18256, also it could be an order migrations issue. – jorlugaqui Sep 12 '15 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 – Apostolos Sep 12 '15 at 20:22
  • Glad it works, yeah using migrate command is the right way now. – jorlugaqui Sep 12 '15 at 20:34
  • It doesn't work...My problems is when testing ./migrate test gives me the error – Apostolos Sep 12 '15 at 20:45
  • Sorry I had understood wrong, so has your db user the right permissions (creating, deleting dbs)? – jorlugaqui Sep 12 '15 at 21:32
  • I have all priviledges on the production database. Doesn't that mean that I have them on testing database too? – Apostolos Sep 12 '15 at 22:43
  • Do you have the right order in migrations files? Is MyCustomUser model created before Patient model? – jorlugaqui Sep 13 '15 at 15:12
  • I deleted all migrations. And tried to re create db.(real one not productive). I have edited my post with more description :) – Apostolos Sep 13 '15 at 15:55
  • I will open a new thread since issue not has to do with unittests anymore – Apostolos Sep 13 '15 at 17:23
  • @Apostolos I am having the exact same issue. – Peter M. Elias Nov 25 '15 at 11:04
  • @Apostolos I am having the exact same issue too. did you solve it eventually? – Diansheng Oct 21 '16 at 04:08

1 Answers1

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

Jeroen Vlek
  • 307
  • 4
  • 15