我可以使用 Rails API 执行 INSERT-SELECT 操作吗? [英] Can I perform a INSERT-SELECT operation with the Rails API?

查看:30
本文介绍了我可以使用 Rails API 执行 INSERT-SELECT 操作吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须将一个表中的 BLOB 字段复制到另一个表中,并且我想使用 INSERT-SELECT 查询来实现这一点.

I have to duplicate a BLOB field from one table into another and I want to use a INSERT-SELECT query to achive this.

INSERT INTO target_table (key, data, comment)
    SELECT 'my key', data, 'some comment' FROM source_table

这可以通过 Rails API 完成吗?

当然,我总是可以使用 ActiveRecord::Base.connection 向数据库发送本机查询,但我希望找到一种Rails 方式"来做到这一点.(一个不涉及在我的 Rails 应用程序中实际加载数据的)

Of course I could always use ActiveRecord::Base.connection to send a native query to the database, but I'm hoping to find a "Rails way" to do this. (One which doesn't involve actually loading the data in my Rails application)

推荐答案

这是一个典型的场景,其中使用 ActiveRecord::Base.connection 直接使用 SQL 是有意义和敏感的.正如您所描述的那样,不可能有任何导轨方式.即使有一个,它也必须将其加载到内存中并将其插入涉及两个模型的目标表中;这太疯狂了.

This is a typical scenario where using the SQL directly using ActiveRecord::Base.connection makes sense and sensibility. There can't possibly be any rails way to it as you described. Even if there were to be one, it has to load it in memory and insert it into the target table involving two models; this is insanity.

这篇关于我可以使用 Rails API 执行 INSERT-SELECT 操作吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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