首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Django中使用OneToOneField交叉不同应用程序重置数据库

在Django中使用OneToOneField交叉不同应用程序重置数据库
EN

Stack Overflow用户
提问于 2009-07-02 08:28:12
回答 1查看 437关注 0票数 1

当我运行./manage.py reset app1时,我得到了以下错误

代码语言:javascript
复制
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的模型中声明的。

如何消除此错误?

更新:sql重置命令的输出是:

(app1)

代码语言:javascript
复制
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)

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-29 13:22:38

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

代码语言:javascript
复制
SET foreign_key_checks = 0

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

InnoDB不允许删除由外键约束引用的表,除非设置了foreign_key_checks = 0。当您删除一个表时,它的create语句中定义的约束也会被删除。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1073162

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档