我有:
def change
create_table :orders do |t|
t.string :name
t.text :address,:limit=>500
t.string :email
t.string :pay_type
t.string :phone,:limit=>25
t.integer :hotel_user_id
t.timestamps
end
add_index("hotel_users","hotel_user_id")
end
日志错误:
$ rake db:migrate
== 20140822140639 CreateOrders: migrating ====================================
-- create_table(:orders)
-> 0.0790s
-- add_index("hotel_users", "hotel_user_id")
rake aborted!
StandardError: An error has occurred, all later migrations canceled:
Mysql2::Error: Key column 'hotel_user_id' doesn't exist in table: CREATE INDEX index_hotel_users_on_hotel_user_id` ON 'hot
el_users' ('hotel_user_id') F:/OrderBooking/db/migrate/20140822140639_create_orders.rb:12:in 'change'
c:in 'migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)'
我有一个表hotel_users
,它有很多orders
。我命令表我想设置一个外键。但是我在创建表时遇到上述错误。
答案 0 :(得分:0)
问题是订单表中存在列hotel_user_id
,而您正在hotel_users
表的同一列上创建索引。