如何迁移现有记录以匹配Rails中的新ORM [英] how to migrate existing records to match new ORM in rails

查看:74
本文介绍了如何迁移现有记录以匹配Rails中的新ORM的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我在正在构建的应用程序中更改了模型关系.因为这些更改将在以后部署,所以我需要知道如何将现有数据迁移到新结构.这感觉像一个可怕的前景.

Recently I changed the model relationships in an app I am building. Because these changes will be later deployed I need to know how to migrate existing data to the new structure. This feels like a scary prospect.

我原来的设置是这样的.

my original set up was like this..

  1. 客户有很多位置
  2. 位置属于客户

然后我创建了一个联接表,以便创建一个client has many locations through client_locations.

I then created a join table so that a client has many locations through client_locations.

因此从旧的数据结构中,我在locations数据库中具有client_id.如何将这些数据移动到新结构?

So from the old data structure I have client_id in the locations database. How would one set about moving this data to the new structure?

推荐答案

仅使用SQL即可迁移数据本身.像这样:

Migrating the data itself could be done with just SQL. Something like:

INSERT INTO client_locations (client_id, location_id)
SELECT client_id, id FROM locations;

然后根据需要清理位置表:

Then clean up the locations table if you need to with:

ALTER TABLE locations DROP client_id;

除非您与多对多关系之外还需要维护位置的所有者.

unless you need to maintain an owner of the location in addition to the many to many relationship.

这篇关于如何迁移现有记录以匹配Rails中的新ORM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆