1
我试图更改表的id
场是一个uuid
Rails迁移 - 暂时忽略外键约束?
这里是我的代码:尝试t.remove :id
的时候,因为一个外键约束的
class AddUuidToProjects < ActiveRecord::Migration[5.0]
def up
add_column :projects, :uuid, :string, limit:36, null: false, first: true
add_column :projects, :old_id, :integer
Project.all.each do |p|
p.update!(old_id: p.id)
end
change_table :projects do |t|
t.remove :id
t.rename :uuid, :id
end
execute "ALTER TABLE projects ADD PRIMARY KEY (id);"
Project.all.each do |p|
# has_one image
Image.find(p.old_id).update!(project: p)
# has_many stories
Story.where(project_id: p.old_id).each do |s|
s.update!(project: p)
end
end
end
...
end
这种迁移休息。该错误信息是:
Mysql2::Error: Cannot drop column 'id': needed in a foreign key constraint 'fk_rails_be41fd4bb7' of table 'db_dev.stories': ALTER TABLE `projects` DROP `id`
的事情是,如果整个迁移跑那么我会被交换出去的id
柱用另一个,太固定了外键。那么,有没有办法忽略迁移的限制?
尝试第一次投放的约束,则稍后重新加入http://stackoverflow.com/questions/14122031/how-to-remove-constraints-from-my-mysql-table – jvnill
你也可以将uuid设置为主键,并让主动记录知道。 /api.rubyonrails.org/classes/ActiveRecord/AttributeMethods/PrimaryKey/ClassMethods.html#method-i-primary_key – jvnill