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

Luk*_*ann 13 sqlite postgresql heroku ruby-on-rails-3

我面临一个共同的问题.我的Rails应用程序在我的本地计算机上运行,​​但在部署到heroku后它崩溃了:

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

这是来自heroku日志:

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)):
Run Code Online (Sandbox Code Playgroud)

@user.hotels.empty?创建错误.我知道,sqlite非常宽容,但PostgreSQL不是.这是酒店模型中的外键:user_id :integer

Heroku说:

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 }
Run Code Online (Sandbox Code Playgroud)

迁移如下所示:

class CreateHotels < ActiveRecord::Migration
  def self.up
    create_table :hotels do |t|
      t.string :name
      t.string :vanity_url
      t.integer :user_id
      ....
Run Code Online (Sandbox Code Playgroud)

iwa*_*bed 20

当数据库中的外键不是整数时,通常会发生这种情况.

例如:

LINE 1: SELECT "hotels".* FROM "hotels" WHERE ("hotels".user_id = 1)
                                                                ^
Run Code Online (Sandbox Code Playgroud)

在这种情况下,PostgreSQL期望user_id是一个整数,但它几乎看起来它告诉你它实际上是一个varchar.

我会尝试删除列并再次添加它.