2009-07-02 96 views
1
跨越不同應用

我收到以下錯誤,當我運行./manage.py reset app1重置數據庫具有OneToOneField在Django

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模型(假設應用2) 。我正在使用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; 
+0

您是否嘗試查看django-admin.py sqlreset app2的輸出?它以前如何? – 2009-07-02 08:30:23

回答

2

一個步驟,該步驟可以手動復位更容易是(並避免恐懼ERROR 1217):

SET foreign_key_checks = 0 

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

的InnoDB不允許由FOREIGN KEY約束下降所引用的 表,除非你當你放下 一個表中設置 FOREIGN_KEY_CHECKS = 0,在創建語句中定義的 的約束條件爲 也被丟棄。