如何在 DROP TABLE 之前禁用(我还不知道正确的术语)外键? OR
删除表格的正确方法?
按照提供的here
步骤进行操作再次出现相同的错误 SQLite3 :: ConstraintException:FOREIGN KEY约束失败:DROP TABLE用户。
(byebug) execute "PRAGMA foreign_keys = OFF"
-- execute("PRAGMA foreign_keys = OFF")
-> 0.0009s
[]
(byebug) execute "DROP TABLE users"
-- execute("DROP TABLE users")
*** ActiveRecord::InvalidForeignKey Exception: SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE users
nil
答案 0 :(得分:2)
假设您的用户与posts表相关联,然后创建迁移并删除
class RemoveForeignKey < ActiveRecord::Migration
def change
# remove the old foreign_key
remove_foreign_key :posts, :users
end
end