-1

谁能告诉我如何将此查询转换为 PostgreSQL

路由控制器.rb

@routes = Route.joins([:departure_location, :destination_location]).where("mdm_locations.name like ? or destination_locations_mdm_routes.name like ?" , "%#{k}%", "%#{k}%")

路线.rb(模型)

module Mdm

  class Route < ActiveRecord::Base

    belongs_to :uom
    belongs_to :distance_uom, :class_name => "Uom", :foreign_key => "distance_uom_id"
    belongs_to :location
    belongs_to :departure_location, :class_name => "Location", :foreign_key => "departure"
    belongs_to :destination_location, :class_name => "Location", :foreign_key => "destination"

    has_many :voyages, :dependent => :restrict

    attr_accessible :description, :distance, :distance_uom_id, :departure, :std_consm, :destination, :uom_id

    validates_presence_of :departure, :destination
 end
end



Error :
PG::Error: ERROR:  operator does not exist: integer = character varying
LINE 1: ...NNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_rou...
                                                             ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT COUNT(*) FROM "mdm_routes" INNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_routes"."departure" INNER JOIN "mdm_locations" "destination_locations_mdm_routes" ON "destination_locations_mdm_routes"."id" = "mdm_routes"."destination" WHERE (LOWER(mdm_locations.name) like '%futong%' or LOWER(destination_locations_mdm_routes.name) like '%futong%')
4

1 回答 1

2

您的错误消息说:

运算符不存在:整数 = 字符变化

并指出这部分 SQL:

INNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_routes"."departure"
-- ------------------------------------------------^

结合这些告诉我们,它mdnm_locations.id是一个整数(如预期的那样),但mdm_routes.departure它是一个varchar. 您不能在 SQL 中比较整数和字符串,除非显式转换其中之一以使类型兼容。

您需要修复您的架构,mdm_routes.departure应该是一个整数列,而不是一个字符串。

MySQL 通过尝试猜测您的意图来尝试变得友好,并让您摆脱许多草率的做法。PostgreSQL 试图通过强迫你准确说出你的意思来避免混淆、错误猜测和隐藏的错误来表现友好。

于 2013-10-09T17:21:24.813 回答