我发现了以下错误,当我运行./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命令的输出是:

(APP1)

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;
有帮助吗?

解决方案

一步骤,该步骤可以手动复位更容易是(并避免恐惧ERROR 1217):

SET foreign_key_checks = 0

HTTP:// dev的。 mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html

  

的InnoDB不允许你砸   由一个外国引用的表   KEY约束,除非你做SET   FOREIGN_KEY_CHECKS = 0。当你放下   一个表中,分别为约束   在它的创建语句定义是   也下降了。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top