rails + sqlite - Can't remove columns from database due to foreign key issue

Viewed 688

I'm currently unable to delete a column from a specific table in my database. the error message sqlite3 gives me is:

FOREIGN KEY constraint failed: DROP TABLE 'table_name'

I am not trying to drop a table though, my migration is

remove_column :table_name, :column_name

This is just a string data column, nothing special about it

2 Answers

This issue is due to a limitation in older versions of SQLite. Rails 5 uses a workaround that duplicates the table without the columns to remove, drops the original table, and then renames the duplicated table. But it fails if there are records with foreign keys.

You can either update the SQLite to the latest version or use this patch: https://gist.github.com/javier-menendez/3cfa71452229f8125865a3247fa03d51

I did ruby db:migrate:redo and it worked.

Related