我有以下表格:
路由和Stop中的in是Mysql中的BIGINT(20)类型。迁移失败,因为使用以下命令:
class CreateMappings < ActiveRecord::Migration
def change
create_table :mappings do |t|
t.references :route, index: true, foreign_key: true
t.references :stop, index: true, foreign_key: true
t.timestamps null: false
end
end
end创建具有route_id和stop_id但数据类型INT(11)的表映射。这与BIGINT(20)不兼容。我怎么才能解决这个问题?有什么想法吗?创建外键失败。
错误消息
这是rake db:migrate --trace输出的一部分
**调用db:first_time **调用环境(first_time) **执行环境**调用db:load_config (first_time) **执行db:load_config **执行db:迁移== 20151227194101 CreateMappings:迁移=================================== -create_table(:映射) rake中止!StandardError:发生了一个错误,所有以后的迁移都取消了: Mysql2 2::错误:无法添加外键约束: ALTER
mappings添加约束fk_rails_1b9f715271外键(route_id)引用routes(id)
当我尝试执行上面的语句时(ALTER mappings.)使用MySql客户端,我得到了以下错误:
Failed to add the foreign key constaint. MIssing index for constraint 'fk_rails_1b9f715271' in the referenced table 'routes'.发布于 2016-01-02 11:13:24
例如,如果不希望添加的列是整数,则references方法将接受类型选项。
t.references :route, type: :bigint, index: true, foreign_key: true发布于 2016-01-01 19:23:36
你试过这个表格了吗?
class CreateMappings < ActiveRecord::Migration
def change
create_table :mappings do |t|
t.references :route
t.references :stop
t.timestamps null: false
end
end
add_index(:mappings, :route)
add_index(:mappings, :stop)
endhttps://stackoverflow.com/questions/34556792
复制相似问题