合并Rails数据库

编程入门 行业动态 更新时间:2024-10-11 21:25:17
本文介绍了合并Rails数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我有两个具有相同结构的数据库。表中有一个整数作为Rails中使用的主键。

如果我有一张病人表,我将有一名患者在一个数据库中使用主键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)} end

YMMV依赖关于记录数量和模型之间的关联。

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") } end

YMMV depending on the number of records and the associations between models.

更多推荐

合并Rails数据库

本文发布于:2023-11-25 06:44:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1628721.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数据库   Rails

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!