1

运行时出现以下错误./manage.py reset app1

Error: Error: app1 couldn't be reset. Possible reasons:
  * The database isn't running or isn't configured correctly.
  * At least one of the database tables doesn't exist.
  * The SQL was invalid.
Hint: Look at the output of 'django-admin.py sqlreset app2'. That's the SQL this command     wasn't able to run.
The full error: (1217, 'Cannot delete or update a parent row: a foreign key constraint fails')

具有 OneToOneField 的模型与另一个应用程序中的另一个模型(比如说 app2)。我正在使用 MySQL InnoDB。更准确地说,OneToOneField 是在 app2 的模型中声明的。

我该如何摆脱这个错误?

更新: sqlreset 命令的输出是:

(应用程序1)

BEGIN;
DROP TABLE `app1_instance`;
DROP TABLE `app1_instancegroup`;
CREATE TABLE `app1_instancegroup` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    -- some more fields
)
;
CREATE TABLE `app1_instance` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `belongs_to_id` integer NOT NULL,
    -- some more fields
)
;
ALTER TABLE `app1_instance` ADD CONSTRAINT `belongs_to_id_refs_id_455b868f` FOREIGN KEY (`belongs_to_id`) REFERENCES `app1_instancegroup` (`id`);
CREATE INDEX `app1_instance_belongs_to_id` ON `app1_instance` (`belongs_to_id`);
COMMIT;

(应用程序2)

BEGIN;
DROP TABLE `app2_team`;
CREATE TABLE `app2_team` (
    `id` integer AUTO_INCREMENT NOT NULL PRIMARY KEY,
    `instance_group_id` integer NOT NULL UNIQUE,
    -- some more fields
)
;
ALTER TABLE `app2_team` ADD CONSTRAINT `instance_group_id_refs_id_39493b52` FOREIGN KEY (`instance_group_id`) REFERENCES `app1_instancegroup` (`id`);
COMMIT;
4

1 回答 1

2

可以使手动重置更容易的一个步骤是(并避免可怕的错误 1217):

SET foreign_key_checks = 0

来自http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

InnoDB does not allow you to drop a table that is referenced by a FOREIGN KEY constraint, unless you do SET foreign_key_checks = 0. When you drop a table, the constraints that were defined in its create statement are also dropped.

于 2009-10-29T13:22:38.497 回答