Check if constraint exists before trying to delete
This commit is contained in:
parent
9b01166f0c
commit
6a42de73d1
1 changed files with 13 additions and 7 deletions
|
@ -10,15 +10,21 @@ func init() {
|
||||||
// SQLite doesn't support constraint updates, but it isn't that careful with constraints anyway.
|
// SQLite doesn't support constraint updates, but it isn't that careful with constraints anyway.
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
_, err := tx.Exec("ALTER TABLE message DROP CONSTRAINT message_chat_jid_fkey")
|
res, _ := tx.Query(`SELECT EXISTS(SELECT constraint_name FROM information_schema.table_constraints
|
||||||
if err != nil {
|
WHERE table_name='message' AND constraint_name='message_chat_jid_fkey');`)
|
||||||
return nil
|
var exists bool
|
||||||
}
|
_ = res.Scan(&exists)
|
||||||
_, err = tx.Exec(`ALTER TABLE message ADD CONSTRAINT message_chat_jid_fkey
|
if exists {
|
||||||
|
_, err := tx.Exec("ALTER TABLE message DROP CONSTRAINT IF EXISTS message_chat_jid_fkey")
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
_, err = tx.Exec(`ALTER TABLE message ADD CONSTRAINT message_chat_jid_fkey
|
||||||
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
|
FOREIGN KEY (chat_jid, chat_receiver) REFERENCES portal(jid, receiver)
|
||||||
ON DELETE CASCADE`)
|
ON DELETE CASCADE`)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}}
|
}}
|
||||||
|
|
Loading…
Reference in a new issue