robincEPtion
robincEPtion

Reputation: 47

SQLite3::ConstraintException: FOREIGN KEY constraint failed: DROP TABLE users

How to disable(I don't know the correct term yet) foreign keys before DROP TABLE? OR

correct way to drop a table?

followed steps provided here

Image link

got the same error again 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

Upvotes: 1

Views: 8998

Answers (1)

Anand
Anand

Reputation: 6531

suppose your users is associated with posts table then create a migration and remove

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

Upvotes: 4

Related Questions