如果我有一张病人表,我将有一名患者在一个数据库中使用主键123,另一个患者在其他数据库中使用相同的主键。
您是否建议合并两个数据库的数据?
解决方案将您的数据库与config / database.yml中的条目一起设置,然后生成新的迁移。
使用ActiveRecord :: Base.establish_connection在迁移中的两个数据库之间切换,如下所示:
def self.up ActiveRecord :: Base.establish_connection:development patients = Patient.find(:all) ActiveRecord :: Base.establish_connection:production patients.each {| patient | Patient.create patient.attributes.except(id)} endYMMV依赖关于记录数量和模型之间的关联。
I have two databases with the same structure. The tables have an integer as a primary key as used in Rails.
If I have a patients table, I will have one patient using primary key 123 in one database and another patient using the same primary key in the other database.
What would you suggest for merging the data from both databases?
解决方案Set both your databases up with entries in config/database.yml, then generate a new migration.
Use ActiveRecord::Base.establish_connection to switch between the two databases in the migration like this:
def self.up ActiveRecord::Base.establish_connection :development patients = Patient.find(:all) ActiveRecord::Base.establish_connection :production patients.each { |patient| Patient.create patient.attributes.except("id") } endYMMV depending on the number of records and the associations between models.
更多推荐
合并Rails数据库
发布评论