Ripristino dei database con avere OneToOneField attraversando diverse applicazioni in Django
-
21-08-2019 - |
Domanda
sto ottenendo il seguente errore quando ho eseguito ./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')
con un modello che ha OneToOneField con un altro modello di un'altra applicazione (diciamo app2). Sto usando MySQL InnoDB. Più precisamente, OneToOneField viene dichiarato nel modello di app2.
Come faccio a sbarazzarsi di questo errore?
Aggiornamento: L'output del comando 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;
Soluzione
Un passo che può fare manualmente il ripristino più facile è (ed evitare l'errore terrore 1217):
SET foreign_key_checks = 0
http: // dev. mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html :
InnoDB non consente di escludere una tabella che fa riferimento una ESTERI KEY, a meno che non si fa SET foreign_key_checks = 0. Quando si rilascia una tabella, i vincoli che erano definito nella sua dichiarazione creare sono anche caduto.