ruby-on-railssqlitesqlite3-rubydrop-table

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

Solution

  • 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