OnetoOnefield가 Django에서 다른 응용 프로그램을 건너는 데이터베이스 재설정

StackOverflow https://stackoverflow.com/questions/1073162

문제

실행할 때 다음과 같은 오류가 발생합니다 ./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;

(APP2)

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;
도움이 되었습니까?

해결책

수동으로 재설정 할 수있는 한 단계는 (그리고 무서운 오류를 피하십시오 1217) :

SET foreign_key_checks = 0

에서 http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-onspraints.html:

InnoDB는 외국 _key_checks = 0을 설정하지 않는 한 외국 키 제약 조건으로 참조되는 테이블을 삭제할 수 없습니다. 테이블을 떨어 뜨릴 때 생성 문에 정의 된 제약 조건도 삭제됩니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top