PG::UndefinedFunction: ERROR: function get_nearest_vertex_to_lon_lat(double precision, double precision) does not exist

PG::UndefinedFunction: ERROR: function get_nearest_vertex_to_lon_lat(double precision, double precision) does not exist

我遇到了一个奇怪的问题,我创建了一个基于 this suggestion 的自定义 SQL 函数。我有一个模型 Route,其控制器操作 create 在模型

上调用此方法
def get_route(startx, starty, endx, endy)

  query = "SELECT seq, cost, slope::double precision, designacao::character varying(192), length::double precision," +
    "ST_AsGeoJSON(geom) FROM pgr_dijkstra('" +
      "SELECT gid AS id," +
               "source::integer," +
               "target::integer," +
               "length AS cost " + 
              "FROM ways WHERE CAST(classif as int) <> 2'," +
      "get_nearest_vertex_to_lon_lat(CAST(#{startx} as float) , CAST(#{starty} as float)), " + 
      "get_nearest_vertex_to_lon_lat(CAST(#{endx} as float), CAST(#{endy} as float)), false, false) a LEFT JOIN ways b ON (a.id2 = b.gid);"

  results = ActiveRecord::Base.connection.execute(query)
  return results
end

这适用于开发但不适用于生产,它给出了 PG::UndefinedFunction: ERROR: function get_nearest_vertex_to_lon_lat(double precision, double precision) does not exist

还有 ActiveRecord::StatementInvalid (PG::UndefinedFunction: ERROR: function get_nearest_vertex_to_lon_lat(double precision, double precision) does not exist

奇怪的是我在 postgres 上执行这个查询并且它有效。

事实证明,尽管我的生产配置 database.yml,但我并不知道 Heroku 创建了自己的生产数据库,所以显然不知道任何自定义 sql 函数,甚至表.

为了将来参考,如果您要部署到 heroku 并且您使用的是远程数据库,则必须执行 然后设置您自己的 DATABASE_URL