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: 
←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m 33: ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m 34: ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m 32:
←[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:
  • ←[32m2012-01-09T19:52:24+00:00 app[web.1]:←[0m 35:
      ←[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  :destroy attr_accessible, :reviews_attributes accepts_nested_attributes_for :reviews, :reject_if => lambda { |a| a[:name].blank? }, :allow_destroy => true end 

    show.html.rb

       'various1', :class => 'review' %> 

    令我困惑的是,我有两个几乎相同的模型,但它们运作良好。

    谢谢!

    你的问题在这里:

     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)'