我可以在Ruby on Rails上编写PostgreSQL函数吗? [英] Can I write PostgreSQL functions on Ruby on Rails?

查看:89
本文介绍了我可以在Ruby on Rails上编写PostgreSQL函数吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在开始一个基于Ruby on Rails的项目。我们曾经使用过Perl和PostgreSQL函数,以及Rails和Active Record,我还没有看到我们应该如何在PostgreSQL中创建函数,并使用Active Record和模型保存记录。



我知道我们可以在PostgreSQL中手动创建它,但Active Record的魔力在于可以使用所有模型重新创建数据库。 b
$ b

有什么方法可以使用Rails创建PostgreSQL函数并将其保存在模型中?

解决方案

您的问题的这部分内容:


我知道我们可以在PostgreSQL中手动创建它,但Active Record的魔术是数据库可以用所有模型重新创建。


告诉我你正在寻找一种将PostgreSQL函数与普通的Rails迁移过程和Rake任务(如 db:schema:load



在迁移中添加和删除函数很简单:

  def up 
connection.execute(%q {
创建或替换函数...
})
结束

结束
连接。执行(%q {
drop function ...
})
end

您需要使用单独的向上向下方法而不是单个更改方法,因为ActiveRecord不知道如何应用,更不用说反转函数creat离子。并使用 connection.execute 将原始函数定义提供给PostgreSQL。



然而, schema.rb 以及与 schema.rb (例如 db:schema:load db:schema:dump )不知道如何处理PostgreSQL函数和ActiveRecord不理解的其他内容。有一种解决方法,你可以选择使用 structure.sql 文件而不是 schema.rb 设置:

  config.active_record.schema_format =:sql 
config / application.rb 文件中输入p>

。之后, db:migrate 会写入一个 db / structure.sql 文件(这只是一个原始的SQL转储文件你的PostgreSQL数据库没有你的数据)而不是 db / schema.rb 。您还将使用不同的Rake任务来处理 structure.sql




  • db:structure:dump 而不是 db:schema:dump

  • db:structure:load 而不是 db:schema:load



其他所有工作都应该一样。



这种方法还可以让您在数据库中使用ActiveRecord无法理解的其他内容: CHECK约束,触发器,非简单的列默认值,...


We are starting a project based on Ruby on Rails. We used to work with Perl and PostgreSQL functions, and with Rails and Active Record I've not seen how we are supposed to create functions in PostgreSQL and keep the record with Active Record and models.

I know we can create it manually in PostgreSQL, but the "magic" with Active Record is that the database can be recreated with all the models.

Is there any way to create the PostgreSQL function using Rails and keep it in the models?

解决方案

This part of your question:

I know we can create it manually in PostgreSQL, but the "magic" with Active Record is that the database can be recreated with all the models.

tells me that you're really looking for a way to integrate PostgreSQL functions with the normal Rails migration process and Rake tasks such as db:schema:load.

Adding and removing functions in migrations is easy:

def up
  connection.execute(%q{
    create or replace function ...
  })
end

def down
  connection.execute(%q{
    drop function ...
  })
end

You need to use separate up and down methods instead of a single change method because ActiveRecord will have no idea how to apply let alone reverse a function creation. And you use connection.execute to feed the raw function definition to PostgreSQL.

However, schema.rb and the usual Rake tasks that work with schema.rb (such as db:schema:load and db:schema:dump) won't know what to do with PostgreSQL functions and other things that ActiveRecord doesn't understand. There is a way around this though, you can choose to use a structure.sql file instead of schema.rb by setting:

config.active_record.schema_format = :sql

in your config/application.rb file. After that, db:migrate will write a db/structure.sql file (which is just a raw SQL dump of your PostgreSQL database without your data) instead of db/schema.rb. You'll also use different Rake tasks for working with structure.sql:

  • db:structure:dump instead of db:schema:dump
  • db:structure:load instead of db:schema:load

Everything else should work the same.

This approach also lets you use other things in your database that ActiveRecord won't understand: CHECK constraints, triggers, non-simple-minded column defaults, ...

这篇关于我可以在Ruby on Rails上编写PostgreSQL函数吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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