Flush contenttypes and auth permissions in Django
December 4th, 2022
I was trying to migrate from MySQL to PostgreSQL using loaddata and dumpdata. For this to work, everything needs to be flushed, but the command:
python manage.py sqlflush --database=postgresql
Flush everything except the contenttypes and a few other (mainly auth related) tables.
So we inevitably end up with conflicts and errors:
IntegrityError: (1062, "Duplicate entry '...' for key 2")
The solution is to manually run the SQL in the postgres shell:
TRUNCATE "django_content_type" CASCADE;
We use CASCADE because django_content_type referenced in a foreign key constraint in auth_permission, which is referenced in auth_group_permissions, etc.
Note:
Access the postgres shell with the Django management command:
python manage.py dbshell --database=postgresql
Unless you are in an environment where you don't have access to psql such as Docker.
In Docker you would run:
# Get the CONTAINER ID of postgresql
docker ps
# Open a bash
docker exec -it 04cd7d7c20e9 bash
# Invoke the database shell
psql -U postgres
# Run the command
TRUNCATE "django_content_type" CASCADE;
Recent TILs
X (Twitter) Automation with Python
Automating social media accounts can be annoying, especially because of the shenanigans Big Social put us through to obtain our API keys. Here is an updated…
Dockerize your Gatsby Projects
JavaScript-based projects tend to rot quickly. Very often, when I come back to them a few months later, nothing works. Today simply running my Gatsby blog after…
Google Login with Django Allauth
I often find myself needing to implement Google OAuth in Django projects but keep forgetting the exact steps. So, I'm documenting the process here for future…