轨道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

错误信息显示:

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

因此,您在reviews中将trip_id列创建为字符串,而不是整数。这在SQLite中运行良好,因为SQLite的类型系统相当松散,但在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

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

一旦你解决了这个问题,你应该安装PostgreSQL,并将你的开发环境切换到那个环境。在SQLite之上开发并部署到PostgreSQL(或者在一个数据库之上开发并在任何其他数据库之上部署)是一个坏主意,会给你带来各种悲伤和困惑。

您可以将列保留为text/varchar数据类型,并将其强制转换为整数。。。

WHERE "reviews"."trip_id"::int = 32

进行迁移的一种更简单的方法是:

change_column :reviews, :trip_id, 'integer USING CAST(trip_id AS integer)'

最新更新