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 %>

这是从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)):

@ 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 }

迁移如下所示:

class CreateHotels < ActiveRecord::Migration
  def self.up
    create_table :hotels do |t|
      t.string :name
      t.string :vanity_url
      t.integer :user_id
      ....

解决方法

这通常发生在数据库中的外键不是整数时.

例如:

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

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

我会尝试删除列并重新添加.

相关文章

validates:conclusion,:presence=>true,:inclusion=>{...
一、redis集群搭建redis3.0以前,提供了Sentinel工具来监控各...
分享一下我老师大神的人工智能教程。零基础!通俗易懂!风趣...
上一篇博文 ruby传参之引用类型 里边定义了一个方法名 mo...
一编程与编程语言 什么是编程语言? 能够被计算机所识别的表...
Ruby类和对象Ruby是一种完美的面向对象编程语言。面向对象编...