krishan
krishan

Reputation: 25

how to start afresh with database in django. Sqlflush and sqlclear don't seem to work

I have 6 models in two files(benef and petition) in addition to the user information. There are some inter dependencies. When I changed the model, there was some error. so I thought of starting afresh and want to drop all tables.

I ran sqlflush and Sqlclear with following result.

Sqlflush result is

BEGIN;
DELETE FROM "django_admin_log";
DELETE FROM "auth_permission";
DELETE FROM "auth_group";
DELETE FROM "auth_group_permissions";
DELETE FROM "django_session";
DELETE FROM "auth_user_groups";
DELETE FROM "auth_user_user_permissions";
DELETE FROM "benef_beneficiary_information";
DELETE FROM "petition_employer";
DELETE FROM "petition_job";
DELETE FROM "nashvegas_migration";
DELETE FROM "benef_beneficiary";
DELETE FROM "auth_user";
DELETE FROM "benef_beneficiaryname";
DELETE FROM "petition_petition";
DELETE FROM "django_content_type";

COMMIT;
Finished "C:\pyProjs\immiFile\manage.py sqlflush" execution.

Sqlclear benef result is 

BEGIN;
DROP TABLE "benef_beneficiary_information";
DROP TABLE "benef_beneficiary";
DROP TABLE "benef_beneficiaryname";

COMMIT;
Finished "C:\pyProjs\immiFile\manage.py sqlclear benef" execution.

sqlclear petition result is 
BEGIN;
DROP TABLE "petition_petition";
DROP TABLE "petition_job";
DROP TABLE "petition_employer";

COMMIT;
Finished "C:\pyProjs\immiFile\manage.py sqlclear petition" execution.

But then when I run the project and go to admin, I still see the old tables and when I click on them, the error related to field comes, which was originally caused by model change. The data is not relevant

OperationalError at /admin/benef/beneficiary/
no such column: benef_beneficiary.last_edited_by_id

I want to start afresh. What is the solution?

I am using Django 1.8 and Python 2.7

Upvotes: 0

Views: 2166

Answers (2)

Burhan Khalid
Burhan Khalid

Reputation: 174614

I want to start afresh. What is the solution?

The sure fire way, which will delete everything (including your data):

  1. Shut down the application (if you are using runserver, make sure its stopped)
  2. Delete the database from the database server. If you are using sqlite, simply delete the db file.
  3. Delete all migration directories created under your app(s).
  4. Type ./manage.py makemigrations
  5. Type ./manage.py migrate

Upvotes: 0

solarissmoke
solarissmoke

Reputation: 31404

From the documentation:

sqlflush: Prints the SQL statements that would be executed for the flush command.

It just prints the statements that would be executed. It doesn't touch the database (same goes for sqlclear). You need to use flush instead.

Also note this from the documentation on flush:

Removes all data from the database and re-executes any post-synchronization handlers. The table of which migrations have been applied is not cleared.

If you would rather start from an empty database and re-run all migrations, you should drop and recreate the database and then run migrate instead.

Upvotes: 2

Related Questions