remove_reference 和 add_reference 的向下迁移报告没有外键
remove_reference and down migration of add_reference reports no foreign key
正在迁移此迁移
def change
remove_reference :order_items, :order, foreign_key: true
end
或向下迁移此迁移
def change
add_reference :order_items, :order, foreign_key: true
end
抛出此错误:
StandardError: An error has occurred, this and all later migrations canceled:
Table 'order_items' has no foreign key for {:to_table=>"orders"}
G:/Rails/learn_rails/db/migrate/20170222035809_move_order_items_under_restaurant_orders.rb:3:in `change'
G:/Rails/learn_rails/bin/rails:4:in `require'
G:/Rails/learn_rails/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
ArgumentError: Table 'order_items' has no foreign key for {:to_table=>"orders"}
G:/Rails/learn_rails/db/migrate/20170222035809_move_order_items_under_restaurant_orders.rb:3:in `change'
G:/Rails/learn_rails/bin/rails:4:in `require'
G:/Rails/learn_rails/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
My order_items
table does have order_id
column (and it is indexed) and I do have orders
table, but then why isn't rails finding the column order_id
and dropping it?
我暂时解决了
remove_index :order_items, :order_id
remove_column :order_items, : order_id
但是如果我必须向下迁移怎么办add_reference
?
order_items
table 有 order_id
列和索引 index_order_items_on_order_id
。
但是它没有 FOREIGN KEY 正如错误明确指出的那样。
如果如图所示存在数据库,请在数据库中进行交叉检查(有许多可用工具)。外键是 fk_rails_hash
形式的外键
在我的例子中,外键在不正确的导出转储期间丢失了,当我导入那个 sql_dump 并将我的数据迁移到这个数据库时,它们不存在,尽管索引和列被保留了。
解决方案:
删除列并以正确的方式添加引用
remove_index :order_items, :order_id
remove_column :order_items, :order_id
add_reference :order_items, :order, foreign_key: true
# or add only the foreign the key constraint
# i'm afraid it I would miss anything else doing so
如果缺少太多外键,最佳解决方案
# instead of whole sql_dump
# export only the data
rails db:migrate:reset
# import only the data making sure the insert
# happens in the right order as it may violate
# foreign_key constraints that are newly added
正在迁移此迁移
def change
remove_reference :order_items, :order, foreign_key: true
end
或向下迁移此迁移
def change
add_reference :order_items, :order, foreign_key: true
end
抛出此错误:
StandardError: An error has occurred, this and all later migrations canceled:
Table 'order_items' has no foreign key for {:to_table=>"orders"}
G:/Rails/learn_rails/db/migrate/20170222035809_move_order_items_under_restaurant_orders.rb:3:in `change'
G:/Rails/learn_rails/bin/rails:4:in `require'
G:/Rails/learn_rails/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
ArgumentError: Table 'order_items' has no foreign key for {:to_table=>"orders"}
G:/Rails/learn_rails/db/migrate/20170222035809_move_order_items_under_restaurant_orders.rb:3:in `change'
G:/Rails/learn_rails/bin/rails:4:in `require'
G:/Rails/learn_rails/bin/rails:4:in `<top (required)>'
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
My
order_items
table does haveorder_id
column (and it is indexed) and I do haveorders
table, but then why isn't rails finding the columnorder_id
and dropping it?
我暂时解决了
remove_index :order_items, :order_id
remove_column :order_items, : order_id
但是如果我必须向下迁移怎么办add_reference
?
order_items
table 有 order_id
列和索引 index_order_items_on_order_id
。
但是它没有 FOREIGN KEY 正如错误明确指出的那样。
如果如图所示存在数据库,请在数据库中进行交叉检查(有许多可用工具)。外键是 fk_rails_hash
形式的外键在我的例子中,外键在不正确的导出转储期间丢失了,当我导入那个 sql_dump 并将我的数据迁移到这个数据库时,它们不存在,尽管索引和列被保留了。
解决方案:
删除列并以正确的方式添加引用
remove_index :order_items, :order_id remove_column :order_items, :order_id add_reference :order_items, :order, foreign_key: true # or add only the foreign the key constraint # i'm afraid it I would miss anything else doing so
如果缺少太多外键,最佳解决方案
# instead of whole sql_dump # export only the data rails db:migrate:reset # import only the data making sure the insert # happens in the right order as it may violate # foreign_key constraints that are newly added