SQLite3 :: ConstraintException:FOREIGN KEY约束失败:DROP TABLE用户

时间:2018-04-02 13:19:22

标签: ruby-on-rails sqlite sqlite3-ruby drop-table

如何在 DROP TABLE 之前禁用(我还不知道正确的术语)外键? OR

删除表格的正确方法?

按照提供的here

步骤进行操作

Image link

再次出现相同的错误 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

1 个答案:

答案 0 :(得分:2)

假设您的用户与posts表相关联,然后创建迁移并删除

class RemoveForeignKey < ActiveRecord::Migration
  def change
    # remove the old foreign_key
    remove_foreign_key :posts, :users
  end
end