Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: operator does not exist: character varying = integer

I face a common problem. My Rails app works on my local machine, but after deploying to heroku it crashes:

<% unless @user.hotels.empty? %>
  <% @user.hotels.each do |hotel| %>
    <%= "#{hotel.description} #{hotel.name} in #{hotel.city}, #{hotel.country}" %><br />
  <% end %>
<% end %>

This is from the heroku logs:

ActionView::Template::Error (PGError: ERROR:  operator does not exist: character varying = integer
LINE 1: SELECT "hotels".* FROM "hotels" WHERE ("hotels".user_id = 1)
                                                                ^
HINT:  No operator matches the given name and argument type(s). You might need to add explicit type casts.
: SELECT "hotels".* FROM "hotels" WHERE ("hotels".user_id = 1)):

@user.hotels.empty? creates the error. I know, sqlite is pretty forgiving, but PostgreSQL is not. This is the foreign key in the hotel model: user_id :integer

Heroku says:

Make sure the operator is adequate for the data type. ActiveRecord does this automatically when you use an interpolated condition form.

Array conditions:
:conditions => ['column1 = ? AND column2 = ?', value1, value2]

Hash conditions:
:conditions => { :column1 => value1, :column2 => value2 }

The migration looks like following:

class CreateHotels < ActiveRecord::Migration
  def self.up
    create_table :hotels do |t|
      t.string :name
      t.string :vanity_url
      t.integer :user_id
      ....
like image 794
Lukas Hoffmann Avatar asked Feb 24 '11 22:02

Lukas Hoffmann


1 Answers

This typically happens when the foreign key in the database is not an integer.

For example:

LINE 1: SELECT "hotels".* FROM "hotels" WHERE ("hotels".user_id = 1)
                                                                ^

In this case, PostgreSQL expects user_id to be an integer, but it almost looks like it's telling you that it was actually a varchar.

I would try removing the column and adding it again.

like image 164
iwasrobbed Avatar answered Oct 30 '22 20:10

iwasrobbed