Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Can anyone tell me how to convert this query to 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 (model)

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%')
share|improve this question
    
i try to this query @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}%") but i get error PG::Error: ERROR: operator does not exist: integer = character varying LINE 1: ...NNER JOIN "mdm_locations" ON "mdm_locations"."id" = "mdm_rou... –  dompuAmanat Fadilla Oct 8 '13 at 19:29
    
Yeah, you should definitely edit the question and post the full error. You ran out of characters on your comment and we can't see the entire error. –  Jeremy Green Oct 9 '13 at 3:31
    
i have edit my question so what happen here and how to convert it, 'cause in mysql it's okay –  dompuAmanat Fadilla Oct 9 '13 at 14:40

1 Answer 1

up vote 2 down vote accepted

Your error message says:

operator does not exist: integer = character varying

and points you at this part of the SQL:

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

Combining those tells us that mdnm_locations.id is an integer (as expected) but mdm_routes.departure is a varchar. You can't compare integers and strings in SQL without explicitly casting one of them to make the types compatible.

You need to fix your schema, mdm_routes.departure should be an integer column, not a string.

MySQL tries to be friendly by attempting to guess your intent and lets you get away with a lot of sloppy practices. PostgreSQL tries to be friendly by forcing you to say exactly what you mean to avoid confusions, incorrect guesses, and hidden bugs.

share|improve this answer
    
I did it already and work, but thanks anyway –  dompuAmanat Fadilla Oct 9 '13 at 19:17

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.