【问题标题】:syntax error at or near "{" using rails in postgresql [closed]在postgresql中使用rails在“{”处或附近出现语法错误[关闭]
【发布时间】:2016-01-27 08:24:08
【问题描述】:

以下查询在 Mysql 中有效,但在 postgresql 中无效。

Campaign.scheduled_with_community_ids(community_ids).
        joins(:community).
        order('FIELD(campaigns.id, #{editable_ids}) DESC').
        order(:launch_date, 'communities.community_name')

正在低于错误

ActiveRecord::StatementInvalid - PG::SyntaxError: ERROR:  syntax error at or near "{" LINE 2: ...s" = 's_approved')) ORDER BY FIELD(campaigns.id, #{editable_...

请任何人帮助我

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 postgresql


    【解决方案1】:

    当你想使用插值时,你应该使用双引号:

    Campaign.scheduled_with_community_ids(community_ids).
        joins(:community).
        order("FIELD(campaigns.id, #{editable_ids}) DESC").
        order(:launch_date, 'communities.community_name')
    

    【讨论】:

      【解决方案2】:

      不要使用single qoute 试试double qoute:

      Campaign.scheduled_with_community_ids(community_ids).
          joins(:community).
          order("FIELD(campaigns.id, #{editable_ids}) DESC").
          order(:launch_date, 'communities.community_name')
      

      【讨论】:

        【解决方案3】:

        改变这个

        Campaign.scheduled_with_community_ids(community_ids).
                joins(:community).
                order('FIELD(campaigns.id, #{editable_ids}) DESC').
                order(:launch_date, 'communities.community_name')
        

        到这里

        Campaign.scheduled_with_community_ids(community_ids).
                joins(:community).
                order("FIELD(campaigns.id, #{editable_ids}) DESC").
                order(:launch_date, 'communities.community_name')
        

        你必须使用双引号才能插入

        【讨论】:

        • 好吧,即使我遇到了一些不同的错误 -------------- ActiveRecord::StatementInvalid - PG::UndefinedFunction: ERROR: function field(bigint, integer, integer , integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer) 不存在 LINE 2: ...OR "campaigns"."status" = ' s_approved')) ORDER BY FIELD(camp...
        • 您能否更新问题并解释更多您要完成的工作
        • Campaign.scheduled_with_community_ids(community_ids)。加入(:社区)。订单(“字段(campaigns.id,#{editable_ids})DESC”)。 order(:launch_date, 'communities.community_name') ----------------------- 但我收到此错误 ActiveRecord::StatementInvalid - PG::UndefinedFunction: ERROR : function field(bigint, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer, integer) 不存在 LINE 2: ...OR "campaigns"."status" = 's_approved')) ORDER BY FIELD(camp...
        • 我认为Postgresql 没有field() 函数,如果有你必须先激活它。
        猜你喜欢
        • 1970-01-01
        • 2021-12-24
        • 1970-01-01
        • 2020-12-26
        • 2020-02-05
        • 2021-12-13
        • 1970-01-01
        • 2013-09-10
        • 1970-01-01
        相关资源
        最近更新 更多