ruby-on-rails – Rails 3.1 Heroku PGError:运算符不存在:字符变化=整数

修复错误有一点麻烦

所有在本地机器上都很出色。
在PG上,heroku是错误。

以下是日志:

  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m ActionView::Template::Error (PGEr
  ror: ERROR:  operator does not exist: character varying = integer
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m LINE 1: ...T "reviews".* FROM "re
  views"  WHERE "reviews"."trip_id" = 32
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m : SELECT "reviews".* FROM "review
  s"  WHERE "reviews"."trip_id" = 32):
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m     31:   <div style='display:non
  e'>
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m     33:      <% for review in @tr
  ip.reviews %>
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m     34:
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m     32:    <div id="inline">
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m HINT:  No operator matches the gi
  ven name and argument type(s). You might need to add explicit type casts.
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m   app/controllers/trips_controlle
  r.rb:21:in `show'
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m cache: [GET /trips/32] miss
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m     36:     <li> <%= review.conte
  nt %> </li>
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m     35:     <ul>
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m   app/views/trips/show.html.erb:3
  3:in `_app_views_trips_show_html_erb__3301405670044045300_69859019468960'
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m Completed 500 Internal Server Err
  or in 86ms
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m   Parameters: {"id"=>"32"}
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m   Processing by TripsController#s
  how as HTML
  ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m Rendered trips/show.html.erb with
  in layouts/application (81.8ms)

不确定在哪里,错误发生和如何解决它。

reviews.rb

 class Review < ActiveRecord::Base
  belongs_to :trip
 end

 class Trip < ActiveRecord::Base
  has_many :reviews, :dependent => :destroy
  attr_accessible, :reviews_attributes

  accepts_nested_attributes_for :reviews, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true
 end 

show.html.rb

 <%= link_to "Read Reviews", '#inline',  :id => 'various1', :class => 'review' %>  

 <div style='display:none'>  
   <div id="inline">
      <% for review in @trip.reviews %>  
       <ul>
         <li> <%= review.content %> </li>
         <li> <i> <%= review.name %> </i> </li>
       </ul>
     <% end %>  
  </div> 
 </div>

令我困惑的是我有两个其他几乎相同的型号,但是它们的效果很好。

谢谢!

你的问题在这里:

WHERE "reviews"."trip_id" = 32

错误消息说:

operator does not exist: character varying = integer

所以你在评论中创建了你的trip_id列,而不是整数。因为SQLite的类型系统相当松散,所以在SQLite中可以正常工作,但是PostgreSQL的PostgreSQL比较复杂,因此在PostgreSQL中不起作用。

您可以尝试添加迁移以修复trip_id的类型:

def change
  change_column :reviews, :trip_id, :integer
end

如果不行,那么删除并重新创建表:

def change
  drop_table :reviews
  create_table :reviews do |t|
    #...
    t.integer :trip_id
    #...
  end
end

如果您有要保留的数据,并且change_column不起作用,则还可以通过原始SQL执行ALTER TABLE:

def change
  execute %q{
    alter table reviews
    alter column trip_id
    type int using cast(trip_id as int)
  }
end

这应该在PostgreSQL(但不是SQLite)中工作,只要您的trip_id中没有任何破损的数据。

一旦你整理出来,你应该安装PostgreSQL并将开发环境切换到那个。在SQLite之上开发并部署到PostgreSQL(或者在一个数据库之上开发并部署在任何其他数据库之上)是一个坏主意,并会导致各种各样的悲伤和混乱。

翻译自:https://stackoverflow.com/questions/8795086/rails-3-1-heroku-pgerror-operator-does-not-exist-character-varying-integer

转载注明原文:ruby-on-rails – Rails 3.1 Heroku PGError:运算符不存在:字符变化=整数