【发布时间】:2013-10-08 19:29:04
【问题描述】:
谁能告诉我如何将此查询转换为 PostgreSQL
routes_controller.rb
@routes = Route.joins([:departure_location, :destination_location]).where("mdm_locations.name like ? or destination_locations_mdm_routes.name like ?" , "%#{k}%", "%#{k}%")
routes.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%')
【问题讨论】:
-
我尝试这个查询 @routes = Route.joins([:departure_location, :destination_location]).where("LOWER(mdm_locations.name) like ? or LOWER(destination_locations_mdm_routes.name) like ?" , "%#{k.downcase}%", "%#{k.downcase}%") 但我得到错误 PG::Error: ERROR: operator does not exist: integer = character varying LINE 1: ...NNER加入 "mdm_locations" ON "mdm_locations"."id" = "mdm_rou...
-
是的,您绝对应该编辑问题并发布完整的错误。您的评论中没有字符,我们看不到整个错误。
-
我已经编辑了我的问题,所以这里发生了什么以及如何转换它,因为在 mysql 中没关系
标签: mysql ruby-on-rails postgresql rails-activerecord