2009-07-02 1 views
1

./manage.py reset app1를 실행할 때 다음과 같은 오류가 발생합니다.Django에서 OneToOneField가 다른 응용 프로그램을 통과하여 데이터베이스 재설정

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 모델에서 선언됩니다.

이 오류를 제거하려면 어떻게합니까?

업데이트 :

(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; 
+0

django-admin.py sqlreset app2의 출력을 살펴 보았습니까? 그게 뭐야? –

+0

업데이트했습니다. – Achimnol

+0

MySQL 콘솔을 통해 외래 키 제약 조건을 수동으로 삭제해야했습니다. Django에서 이러한 종류의 작업에 대한 지원이 있습니까? – Achimnol

답변

2

한 걸음 (두려움을 피하십시오. 오류 1217) :

에서 363,210
SET foreign_key_checks = 0 

:

이노 당신이 테이블 놓으면 당신이 FOREIGN_KEY_CHECKS = 0을 설정하는 경우가 아니면, 외국 KEY 제약 조건에 의해 참조되는 테이블을 삭제하는 것을 허용하지 않습니다, 해당 명령문에 정의 된 인 제약 조건도 입니다.