Ruby on rails 由于我很久以前写的迁移的影响,我犯了一个错误
我想添加新列'piece_id',并将其添加到Bucket表中Ruby on rails 由于我很久以前写的迁移的影响,我犯了一个错误,ruby-on-rails,ruby,Ruby On Rails,Ruby,我想添加新列'piece_id',并将其添加到Bucket表中 class AddPieceIdToBuckets < ActiveRecord::Migration def change add_column :buckets, :piece_id, :string end end class Bucket < ActiveRecord::Base validates :piece_id, presence: true end 虽然我在以前写的迁移中没有p
class AddPieceIdToBuckets < ActiveRecord::Migration
def change
add_column :buckets, :piece_id, :string
end
end
class Bucket < ActiveRecord::Base
validates :piece_id, presence: true
end
虽然我在以前写的迁移中没有piece_id,但我认为这是在模型中编写验证的影响。及 我为迁移创建了几个我以前写过的对象。我应该编辑它吗
def change
Bucket.reset_column_information
Bucket.create(
name: 'test_name',
about: 'test',
)
end
我已经被这一点困扰了很多次,因为这个原因,我不再在迁移中通过模型进行任何数据更改 由于您可以确信创建Bucket的旧迁移已经运行,因此可以删除该代码位,或者更好地将
piece\u id
添加到属性中,并为其指定一个值,以便在从空数据库迁移时通过
我现在使用rake db:seeds
在db/seeds.rb
中创建数据,我有:
require_relative "seeds/#{Rails.env}/all"
以及目录结构,例如:
db/seeds/development/all.rb
db/seeds/production/all.rb
db/seeds/development/all.rb
db/seeds/production/all.rb
在development/all.rb
中,我确实require\u relative'../production/all'
作为部署的一部分,我运行了
rake db:seed
,效果很好,只需确保seed是幂等的,所以检查记录是否已经存在,或者使用活动记录方法,如find\u或\u create\u by
表中是否存在片段id
列?
db/seeds/development/all.rb
db/seeds/production/all.rb