【问题标题】:SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE usersSQLite3::ConstraintException:外键约束失败:删除表用户
【发布时间】:2018-09-11 17:15:03
【问题描述】:

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

删除表的正确方法?

提供的以下步骤here

Image link

再次遇到同样的错误SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE users.

(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

【问题讨论】:

  • @robinEPtion 你想删除/删除/删除表或外键吗?
  • @AayQue 删除表
  • @robincEPtion 你在使用迁移来下垂表吗?
  • @robincEption 而不是您需要先删除此表的值,这些值作为外键存储在另一个表中。
  • 是的@Gabbar,我应该在迁移期间这样做。

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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2013-01-20
    • 1970-01-01
    • 2013-03-04
    • 2018-06-10
    • 1970-01-01
    • 2012-06-21
    • 1970-01-01
    • 1970-01-01
    • 2016-11-08
    相关资源
    最近更新 更多