Ticket #372 (closed defect: fixed)

Opened 5 years ago

Last modified 4 years ago

initial_data is imported before necessary migrations have run, causing a stacktrace in manage.py test

Reported by: anonymous Owned by: andrew
Priority: minor Milestone: 0.7
Component: commands Version: 0.6.2
Keywords: Cc:

Description

There's something a bit screwy about the order in which South applies migrations and attempts to load initial_data.json fixtures. Here's an editid transcript of our test runner:

$ ./manage.py test myapp
Creating test database 'default'...
Syncing...
[...]
Migrating...
Running migrations for myapp:
 - Migrating forwards to 0011_add_indexes_to_created_on_in_discussion_and_myapp.
 > myapp:0001_initial
 > profiles:0001_add_profile
 > myapp:0002_add_myapp_table
[...]
 > myapp:0011_add_indexes_to_created_on
 - Loading initial data for myapp.
Installing json fixture 'initial_data' from '../../myapp/fixtures'.
Installing json fixture 'initial_data' from '../../profiles/fixtures'.
Problem installing fixture '../../profiles/fixtures/initial_data.json': Traceback (most recent call last):
  File "django/core/management/commands/loaddata.py", line 169, in handle
    obj.save(using=using)
  File "django/core/serializers/base.py", line 165, in save
    models.Model.save_base(self.object, using=using, raw=True)
  File "django/db/models/base.py", line 492, in save_base
    manager.using(using).filter(pk=pk_val).exists())):
  File "django/db/models/query.py", line 491, in exists
    return self.query.has_results(using=self.db)
  File "django/db/models/sql/query.py", line 389, in has_results
    return bool(compiler.execute_sql(SINGLE))
  File "django/db/models/sql/compiler.py", line 740, in execute_sql
    cursor.execute(sql, params)
  File "django/db/backends/postgresql_psycopg2/base.py", line 44, in execute
    return self.cursor.execute(query, args)
DatabaseError: relation "profiles_badge" does not exist
LINE 1: ..._badge"."name", "profiles_badge"."image_url" FROM "profiles_...

Running migrations for profiles:
 - Migrating forwards to 0003_add_badges.
 > profiles:0002_add_profile_fields
 > profiles:0003_add_badges
 - Loading initial data for profiles.
Installing json fixture 'initial_data' from '../../myapp/fixtures'.
Installing json fixture 'initial_data' from '../../profiles/fixtures'.
Installed 3 object(s) from 2 fixture(s)
[...]
Migrated:
 - myapp
 - profiles
.........
----------------------------------------------------------------------
Ran 9 tests in 0.471s

OK

Note the ugly stacktrace in the middle. South attempts to import the initial_data.json from our profiles app BEFORE it has run all of the migrations for that app, so the initial_data import fails due to a missing table. This doesn't cause the tests to fail though, since South then applies the migrations and then attempts to import initial_data.json again, this time succeeding.

The tests still pass, so this isn't a show-stopper - but it's ugly having that stacktrace show up every time the tests run, and it's a bit worrying that South is attempting to import fixtures before the migrations have been executed.

Change History

comment:1 Changed 5 years ago by andrew

  • Status changed from new to infoneeded

Ah, it seems the code which isolates the data importing to only one app is probably not working. What Django version are you running? Have you tried this with 0.7-pre (i.e. trunk)?

comment:2 Changed 5 years ago by andrew

  • Milestone set to 0.7

comment:3 Changed 5 years ago by anonymous

Brilliant - yes, running current trunk fixes it (but a bunch of South tests are failing at the moment so we'll hold off upgrading until the next official release).

comment:4 Changed 5 years ago by andrew

  • Status changed from infoneeded to closed
  • Resolution set to fixed

Right, in that case I'll close it.

(Yes, there's a few failing tests, I haven't yet fixed the ones that broke when I changed the dependency engine. There should be a beta sometime this or next week, with a release shortly after.)

Note: See TracTickets for help on using tickets.