[Fixed]-Database trouble in Django: can't reset because of dependencies

5👍

The easy way to fully reset a Django database is using django-extensions.

It has a reset_db command that supports all Django’s default database backends.

python manage.py reset_db

If you’re using Django 1.2+ you should explicitly define the database you want to reset. If your project only uses one database, you should probably set --router=default

5👍

I use a little unix pipeline that adds CASCADE to all the DROP statements.

python manage.py sqlreset myapp | sed 's/DROP TABLE \(.*\);/DROP TABLE \1 CASCADE;/g' | \
psql --username myusername mydbname

2👍

The problem of DROP TABLE CASCADE is that it just remove a foreign keys on related tables – after syncdb this relation is not recreated.
I found no way to recreate the particular model’s tables, so I’m reseting whole application by recreating schema:

  DROP SCHEMA public CASCADE;
  CREATE SCHEMA "public" AUTHORIZATION "owner of database";

That should work only with database that supports schema, e.g. postgresql

1👍

Using the details in other answers, I made a bash function that I dropped into ~/.bash_profile (on Mac OS X).

django_reset () { python mainsite/manage.py sqlreset "$*" | sed 's/DROP TABLE \(.*\);/DROP TABLE \1 CASCADE;/g' | mainsite/manage.py dbshell ; }

Then just run this command in the terminal from your root code directory (so the path to mainsite/manage.py makes sense).

django_reset myappA myappB

And it’ll execute!

0👍

I found another way. I’m using sqlite3 that comes by default in Django.
To reset table to default.
python manage.py flush –database=default
after this you will need to use the syncdb command again.

Leave a comment