父子表定義
一張 “訂單表 Orders” 中有一個字段 “customer_id” 顧客id,指向顧客表 (Customers) 的主鍵(id字段),那麼 顧客表就叫做“父表”或“被引用表”,而顧客表被稱爲“子表”或“引用表”。
當 drop 父表時,必須先刪除掉“子表”,否則 mysql 會報告:
ERROR 1217 (23000): Cannot delete or update a parent row: a foreign key constraint fails
如果兩張表互相之間相互引用,那麼無論用什麼刪除順序都無法刪除表。
這時可以用設置變量 “” 的方式來解決,即告訴 MySQL 不要檢查外鍵約束了。
SET FOREIGN_KEY_CHECKS = 0;
drop table if exists customers;
drop table if exists orders;
drop table if exists order_details;
SET FOREIGN_KEY_CHECKS = 1;
Grails 的 dbCreate 屬性
當 Grails 的 dbCreate 設置爲 “create-drop” 的時候,正常情況下啓動 Grails 時數據庫表都能被 drop 並重新創建一遍,但是如果有這些外鍵關係存在時會導致 drop 失敗,也就無法重新建表,導致 表結構 不能正確更新。
需要手工按照上面的方法刪除 table 後重新啓動。