question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Wagtail migration doesn't respect --run-syncdb

See original GitHub issue

Running migrate under wagtail creates tables with data despite using --run-syncdb. As a result of this, it is difficult to load a database dump via ./manage.py loaddata. It fails because some tables are not empty. There are open tickets where people come up with unnecessary complicated hacks to load data:

This would all become quite simple if the wagtail migration would respect --run-syncdb

Issue Summary

When running ./manage.py migrate --run-syncdb, some wagtail tables are initialized with data which they should not. At least the following tables are not empty:

  • " wagtailcore_page"
  • " wagtailcore_site"
  • " wagtailcore_groupcollectionpermission"
  • " wagtailcore_grouppagepermission"

Steps to Reproduce

  1. Start a new project with wagtail start myproject
  2. Run ./manage.py migrate --run-syncsb
  3. Inspect tables listed above

Expected Behaviour

When running migrations with --run-syncdb on an empty database, empty tables should be created without any data

Technical details

  • Python version: 3.7.5
  • Django version: 3.0.4
  • Wagtail version: 2.8

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:5

github_iconTop GitHub Comments

2reactions
gasmancommented, Jul 31, 2020

./manage.py migrate followed by ./manage.py flush should give you an empty database with all tables intact.

0reactions
eeintechcommented, May 26, 2020

@gasman On my side I am trying to migrate from SQLite3 to a PostgreSQL database, so I followed this guide: https://gist.github.com/sirodoht/f598d14e9644e2d3909629a41e3522ad

It does say to run it with --run-syncdb option, which in my mind is the way to create empty tables which will be filled up with the loaddata command later on.

I ran into a separate issue (I believe related to #6069) but after configuring my PostgreSQL database as default in my base.py Django settings file, I ran into another error:

Traceback (most recent call last):
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/db/backends/utils.py", line 86, in _execute
    return self.cursor.execute(sql, params)
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "wagtailcore_page_path_key"
DETAIL:  Key (path)=(00010001) already exists.


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

Traceback (most recent call last):
  File "./manage.py", line 20, in <module>
    execute_from_command_line(sys.argv)

(...troncated ...)

django.db.utils.IntegrityError: Problem installing fixture '/home/eeintech/eeintech/datadump.json': Could not load wagtailcore.Page(pk=7): duplicate key value violates unique constraint "wagtailcore_page_path_key"
DETAIL:  Key (path)=(00010001) already exists.

Indeed:

>>> from wagtail.core.models import Page
>>> Page.objects.all()
<PageQuerySet [<Page: Root>, <Page: Welcome to your new Wagtail site!>]>

I deleted the Page instance but still running into a content type error when running loaddata:

django.db.utils.IntegrityError: Problem installing fixture '/home/eeintech/eeintech/datadump_content.json': Could not load contenttypes.ContentType(pk=16): duplicate key value violates unique constraint "django_content_type_app_label_model_76bd3d3b_uniq"
DETAIL:  Key (app_label, model)=(wagtailsearch, query) already exists.

And can’t find a way to delete the initial content load from the shell:

>>> ContentType.objects.all().delete()
Traceback (most recent call last):
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/db/backends/base/base.py", line 243, in _commit
    return self.connection.commit()
psycopg2.errors.ForeignKeyViolation: update or delete on table "django_content_type" violates foreign key constraint "wagtailcore_page_content_type_id_c28424df_fk_django_co" on table "wagtailcore_page"
DETAIL:  Key (id)=(1) is still referenced from table "wagtailcore_page".

So I’ve tried to dump the data exluding content types python manage.py dumpdata --exclude=contenttypes --exclude=auth.Permission > dumpdata_nocontent.json as a workaround (suggested here) but no luck:

$ ./manage.py loaddata datadump_nocontent.json
Traceback (most recent call last):
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 110, in get_for_id
    ct = self._cache[self.db][id]
KeyError: 64

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/wagtail/core/models.py", line 936, in get_indexed_instance
    return self.specific
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/utils/functional.py", line 48, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/wagtail/core/models.py", line 620, in specific
    content_type = ContentType.objects.get_for_id(self.content_type_id)
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/contrib/contenttypes/models.py", line 114, in get_for_id
    ct = self.get(pk=id)
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/db/models/manager.py", line 82, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/home/eeintech/virtualenv/eeintech/3.7/lib/python3.7/site-packages/django/db/models/query.py", line 417, in get
    self.model._meta.object_name
django.contrib.contenttypes.models.DoesNotExist: ContentType matching query does not exist.

Anyway, I digress…

What’s your proposed way to be able to create tables but not load initial data? Better yet, what your proposed way for migrating data from one database to another?

Thanks a lot.

Read more comments on GitHub >

github_iconTop Results From Across the Web

problem: Table doesn't exist( python manage.py migrate)
I dropped some table related to an app. and again tried the syncdb command. python manage.py migrate.
Read more >
[Solved]-Django syncdb not running custom SQL-django
Coding example for the question Django syncdb not running custom SQL-django. ... Initial data sql doesn't get run for South-managed applications.
Read more >
migrate --run-syncdb doesn't work when specifying a database
When running 'python manage.py migrate --run-syncdb polls' I get the error "CommandError: App 'polls' does not have migrations.", while I expected an sqlite- ......
Read more >
Migrating your Django Project to Heroku - Real Python
In this tutorial, we'll be taking a simple local Django project, backed by a MySQL database, and converting it to run on Heroku....
Read more >
Django and Wagtail Migrations - consumerfinance.gov - CFPB
To automatically generate a schema migration, run the following, editing it to give your migration a name that briefly describes the change(s) you're...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found