Navid
Navid

Reputation: 642

Drop tables in database used by django

Problem Statement: I want to drop some tables in a databases.

Error getting: a foreign key constraint fails

Details:

I have a pre-existing (not populated by django models.py) database. I am using django. I used this database for creating django site. I have given its name and proper setting while creating a site in django. There are no models in created in models.py.

After executing command:

python manage.py syncdb

Django added following tables to that database:

| auth_group |

| auth_group_permissions |

| auth_permission |

| auth_user |

| auth_user_groups |

| auth_user_user_permissions |

| django_content_type |

| django_session |

| django_site |

I want to drop all these tables.

I tried this query;

DROP TABLE table_name;

but mysql prompted with error showing:

ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails

Is there some way I can drop only above mentioned tables keep my pre-existing tables intact?

Thank you.

Upvotes: 1

Views: 1804

Answers (2)

Raunak Agarwal
Raunak Agarwal

Reputation: 7228

Instead of using default innodb storage engine you can easily configure django to use MyISAM. In the later case, it wouldn't restrict you from performing various operation because of the relationships. Both have their positive and negative points.

Upvotes: 0

Ghostman
Ghostman

Reputation: 6114

Two possibilities:

  1. There is a table within another schema ("database" in mysql terminology) which has a FK reference
  2. The innodb internal data dictionary is out of sync with the mysql one.

You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.

If it turns out to be the latter case, I'd dump and restore the whole server if you can.

MySQL 5.1 and above will give you the name of the table with the FK in the error message.

and try this too

Please refer this question stackoverflow

Disable foreign key checking

DISABLE KEYS

Do make sure to SET FOREIGN_KEY_CHECKS=1;

Upvotes: 2

Related Questions