2

I have a migration that loads a fixture for populating the database with a basic site structure (from Loading initial data with Django 1.7 and data migrations ). After that migration ran, my test adds a (custom) NewsPage. THis yields an "IntegrityError at /admin/pages/add/website/newspage/5/ duplicate key value violates unique constraint "wagtailcore_page_pkey" DETAIL: Key (id)=(3) already exists." The same happens when i add the page through the admin interface.

It's a bit suspicious that the Page with pk=3 is the first one that is created in my fixture. The other two pk's were already created by Wagtail's migrations.

I've read up about fixtures an migrations, and it seems Postgres won't reset the primary key sequences. I'm assuming this is also my problem here.

I found a possible solution in Django: loaddata in migrations errors, but that failed (with "psycopg2.ProgrammingError: syntax error at or near "LINE 1: BEGIN;"). Trying to execute the gist of it, I ran the sqlsequencereset management command (./manage.py sqlsequencereset wagtailcore myapp), but i still get the error, although now for id=4.

  • Is my assumption correct that Postgres not resetting the primary key sequences is my problem here?
  • Does anyone know how to reliably fix that from/after a migration loaded fixtures?
  • Would it maybe be easier / more reliable to create content in Python code?

Edit (same day):

If i don't follow the example in Loading initial data with Django 1.7 and data migrations, but just run the management command, it works:

def load_fixture(fixture_file):
    """Load a fixture."""
    commands = StringIO()
    call_command('loaddata', fixture_file, stdout=commands)

I don't know what the drawbacks of this more simple approach are.

Edit 2 (also same day):

Ok i do know, the fixtures will be based on the current model state, not the state that the migration is for, so it will likely break if your model changes.

I converted the whole thing to Python code. That works and will likely keep working. Today i learned: don't load fixtures in migrations. (Pity, it would have been a nice shortcut.)

khink
  • 81
  • 4

0 Answers0