Opened 9 years ago

Closed 9 years ago

#24274 closed Uncategorized (invalid)

migrations break fixtures

Reported by: Yurii Zolot'ko Owned by: nobody
Component: Uncategorized Version: 1.7
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

Since Django 1.7 syncdb does not work if there are fixtures including objects from apps having migrations because currently syncdb/migrate does following:

  1. Create tables for unmigrated apps
  2. Load initial_data.json
  3. Apply migrations

If one would like to have instances of models from app having migration in initial_data.json syncdb will break with smth like this:

Operations to perform:
  Synchronize unmigrated apps: google, allauth, widget_tweaks, flatpages, facebook
  Apply all migrations: account, sessions, admin, sites, auth, contenttypes, socialaccount
Synchronizing apps without migrations:
  Creating tables...
    Creating table django_flatpage_sites
    Creating table django_flatpage
  Installing custom SQL...
  Installing indexes...
Traceback (most recent call last):
  File "manage.py", line 10, in <module>
    execute_from_command_line(sys.argv)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 385, in execute_from_command_line
    utility.execute()
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 377, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 288, in run_from_argv
    self.execute(*args, **options.__dict__)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 533, in handle
    return self.handle_noargs(**options)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/commands/syncdb.py", line 27, in handle_noargs
    call_command("migrate", **options)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 128, in handle
    created_models = self.sync_apps(connection, executor.loader.unmigrated_apps)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/commands/migrate.py", line 298, in sync_apps
    call_command('loaddata', 'initial_data', verbosity=self.verbosity, database=connection.alias, skip_validation=True, app_label=app_label, hide_empty=True)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/__init__.py", line 115, in call_command
    return klass.execute(*args, **defaults)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/base.py", line 338, in execute
    output = self.handle(*args, **options)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 61, in handle
    self.loaddata(fixture_labels)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 91, in loaddata
    self.load_label(fixture_label)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/management/commands/loaddata.py", line 148, in load_label
    obj.save(using=self.using)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/core/serializers/base.py", line 173, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 617, in save_base
    updated = self._save_table(raw, cls, force_insert, force_update, using, update_fields)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 679, in _save_table
    forced_update)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/models/base.py", line 723, in _do_update
    return filtered._update(values) > 0
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/models/query.py", line 600, in _update
    return query.get_compiler(self.db).execute_sql(CURSOR)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 1004, in execute_sql
    cursor = super(SQLUpdateCompiler, self).execute_sql(result_type)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/models/sql/compiler.py", line 786, in execute_sql
    cursor.execute(sql, params)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 81, in execute
    return super(CursorDebugWrapper, self).execute(sql, params)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/utils.py", line 94, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/backends/utils.py", line 65, in execute
    return self.cursor.execute(sql, params)
  File "/home/user/myproject/local/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py", line 485, in execute
    return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: Problem installing fixture '/home/user/myproject/initial_data.json': Could not load socialaccount.SocialApp(pk=1): no such table: socialaccount_socialapp

The proper sequence of actions that syncdb/migrate does should be:

  1. Create tables for unmigrated apps
  2. Apply migrations
  3. Load initial_data.json

Change History (2)

comment:1 by Yurii Zolot'ko, 9 years ago

Currently the only way to workaround this is to delete third-party application migrations like this:
rm -rfv local/lib/python2.7/site-packages/allauth/socialaccount/migrations/

Than manage.py migrate and manage.py test will work again.

comment:2 by Tim Graham, 9 years ago

Resolution: invalid
Status: newclosed

Apps with migrations don't support initial_data, so you'll need to find a different solution like using a data migration.

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