Opened 9 years ago

Closed 9 years ago

#25216 closed Bug (needsinfo)

DB migration bug in 1.8.3 when creating ManyToMany relation.

Reported by: Chen Owned by: nobody
Component: Migrations Version: 1.8
Severity: Normal Keywords: migrate
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by Chen)

Hi,
I am using python version 3.4 and django 1.8.3, I have the following very simple code:

from django.contrib.auth.models import User

class Agent(models.Model):

    name = models.CharField(max_length=100, primary_key=True)
    users = models.ManyToManyField(User, related_name='accessible_agents', default=[])

and when I have an empty database, and do python manage.py migrate.

It gives me error, and I tested on 1.7.8, it works fine.

Operations to perform:
  Synchronize unmigrated apps: staticfiles, download, messages
  Apply all migrations: admin, auth, contenttypes, sessions
Synchronizing apps without migrations:
  Creating tables...
    Creating table download_agent
    Running deferred SQL...
Traceback (most recent call last):
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 220, in execute
    self.errorhandler(self, exc, value)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 209, in execute
    r = self._query(query)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 371, in _query
    rowcount = self._do_query(q)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 335, in _do_query
    db.query(q)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
_mysql_exceptions.OperationalError: (1005, "Can't create table 'portal_aperture.#sql-403_b3' (errno: 150)")

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/core/management/__init__.py", line 338, in execute_from_command_line
    utility.execute()
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/core/management/__init__.py", line 330, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/core/management/base.py", line 393, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/core/management/base.py", line 444, in execute
    output = self.handle(*args, **options)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 179, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/core/management/commands/migrate.py", line 317, in sync_apps
    cursor.execute(statement)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/backends/utils.py", line 79, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/utils.py", line 97, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/utils/six.py", line 658, in reraise
    raise value.with_traceback(tb)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/backends/utils.py", line 62, in execute
    return self.cursor.execute(sql)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/django/db/backends/mysql/base.py", line 124, in execute
    return self.cursor.execute(query, args)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 220, in execute
    self.errorhandler(self, exc, value)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
    raise errorvalue
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 209, in execute
    r = self._query(query)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 371, in _query
    rowcount = self._do_query(q)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/cursors.py", line 335, in _do_query
    db.query(q)
  File "/srv/company/virtualenvs/portal_aperture/lib/python3.4/site-packages/MySQLdb/connections.py", line 280, in query
    _mysql.connection.query(self, query)
django.db.utils.OperationalError: (1005, "Can't create table 'portal_aperture.#sql-403_b3' (errno: 150)")

Thanks.

Change History (7)

comment:2 by Chen, 9 years ago

Description: modified (diff)

comment:3 by Chen, 9 years ago

I suspect it is a problem with the order executed, the M2M expects my User model.

I removed all of my models definition in that app, and run python manage.py migrate to generate all those auth, sessions builtin tables, and then paste my models back run migrate again, this time everything works, and all the tables are generated correctly.

Thanks

comment:4 by Tim Graham, 9 years ago

Description: modified (diff)

comment:5 by Chen, 9 years ago

Description: modified (diff)

comment:6 by Tim Graham, 9 years ago

Did you create migrations for your app using makemigrations? If not, you have an unmigrated app depending on a migrated one as described in the docs which is an invalid configuration.

comment:7 by Tim Graham, 9 years ago

Resolution: needsinfo
Status: newclosed

Please reopen with a sample project to reproduce the problem if my last comment is incorrect. Thanks!

Note: See TracTickets for help on using tickets.
Back to Top