加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby-on-rails-3 – 如何在迁移中正确添加自定义SQL DML

发布时间:2020-12-17 03:03:35 所属栏目:百科 来源:网络整理
导读:我今天有一种情况,我希望在迁移中向表中添加部分postgres索引.当然这种东西在使用add_index的轨道中是不可能的(它是 coming some time) 所以,我被迫在迁移中使用execute语句. 现在,schema.rb在顶部有这个注释: # Note that this schema.rb definition is th
我今天有一种情况,我希望在迁移中向表中添加部分postgres索引.当然这种东西在使用add_index的轨道中是不可能的(它是 coming some time)

所以,我被迫在迁移中使用execute语句.

现在,schema.rb在顶部有这个注释:

# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system,you should be using db:schema:load,not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass,the slower it'll run and the greater likelihood for issues).
#

不幸的是,在schema.rb中没有跟踪这个execute语句,如果我有任何自定义DML,这会使schema.rb变得毫无用处.

有什么办法可以强制包含DML的执行语句将自己发现到schema.rb中吗?

解决方法

使用schema.rb无法实现您想要的功能,您要做的是切换到使用数据库的本机模式转储/检索代码.

config.active_record.schema_format = :sql

现在,您将获得一个structure.sql文件,而不是schema.rb文件.这应该透明地适用于所有rails的内置任务,并且因为它使用本机转储器格式,它将支持你想要的任何疯狂的东西.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读