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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5111338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-20 00:34:54  来源:igfitidea点击:

Error: operator does not exist: character varying = integer

ruby-on-rails-3sqlitepostgresqlheroku

提问by Lukas Hoffmann

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

我面临一个常见的问题。我的 Rails 应用程序在我的本地机器上运行,但在部署到 heroku 后它崩溃了:

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

这是来自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?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

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

Heroku says:

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 }

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

回答by iwasrobbed

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_idto be an integer, but it almost looks like it's telling you that it was actually a varchar.

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

I would try removing the column and adding it again.

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