代码之家  ›  专栏  ›  技术社区  ›  mbajur

在db:reset之后postgres函数没有持久化

  •  1
  • mbajur  · 技术社区  · 8 年前

    我用自定义postgres函数创建了一个迁移:

    class CreatePopularityPgFunctions < ActiveRecord::Migration[5.2]
      def up
        execute %{
          CREATE OR REPLACE FUNCTION popularity(count integer, weight integer default 3) RETURNS integer AS $$
            SELECT count * weight
          $$ LANGUAGE SQL IMMUTABLE;
        }
      end
    
      def down
        execute 'drop function popularity(integer, integer) cascade'
      end
    end
    

    跑步 rake db:migrate 正确地将其添加到架构中。但是,跑步 rake db:reset 似乎由于某种原因没有在pg中创建这个函数。该函数不在架构中,如果尝试在SQL查询中使用它,则会导致有关缺少函数的错误。

    1 回复  |  直到 8 年前
        1
  •  3
  •   Yura Zhivaga    8 年前

    db:reset 运行一个 db:drop db:setup . 在 db:setup 创建数据库架构。但是 schema.rb 不处理自定义函数或视图。

    来自文档:

    db/schema.rb无法表示特定于数据库的项,如外键约束、触发器或存储过程。在迁移过程中,可以执行自定义SQL语句,但架构转储程序无法从数据库中重新构造这些语句。如果您使用这样的特性,那么应该将模式格式设置为:sql。

    你可以使用 structure.sql 相反。使用 结构.sql :

    这在config/application.rb中由config.active_record.schema_格式设置设置,可以是:sql或:ruby。

    更多细节 here.

    推荐文章