【问题标题】:PG::UndefinedFunction: ERROR: function get_nearest_vertex_to_lon_lat(double precision, double precision) does not existPG :: UndefinedFunction:错误:函数get_nearest_vertex_to_lon_lat(双精度,双精度)不存在
【发布时间】:2025-12-04 04:30:02
【问题描述】:

我遇到了一个奇怪的问题,我创建了一个基于 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 上执行了这个查询,它可以工作。

【问题讨论】:

    标签: ruby-on-rails postgresql rails-activerecord postgis pg


    【解决方案1】:

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

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

    【讨论】: