emi*_*lsw 23 sqlite postgresql ruby-on-rails heroku
修复错误有点麻烦.
一切都适用于本地机器.在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)
Run Code Online (Sandbox Code Playgroud)
不确定在哪里,错误发生以及如何解决它.
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
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
令我困惑的是,我有两个几乎相同的模型,但它们运作良好.
谢谢!
mu *_*ort 35
你的问题在这里:
WHERE "reviews"."trip_id" = 32
Run Code Online (Sandbox Code Playgroud)
并且错误消息说:
运算符不存在:字符变化=整数
所以你已经用字符串而不是整数创建了trip_id列reviews.这在SQLite中可以正常工作,因为SQLite的类型系统相当松散,但它在PostgreSQL中不起作用,因为PostgreSQL相当严格.
您可以尝试添加迁移来修复以下类型trip_id:
def change
change_column :reviews, :trip_id, :integer
end
Run Code Online (Sandbox Code Playgroud)
如果这不起作用,则删除并重新创建表:
def change
drop_table :reviews
create_table :reviews do |t|
#...
t.integer :trip_id
#...
end
end
Run Code Online (Sandbox Code Playgroud)
如果您有要保留的数据并且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
Run Code Online (Sandbox Code Playgroud)
这应该适用于PostgreSQL(但不适用于SQLite),只要您的数据中没有任何损坏的数据trip_id.
一旦你完成了整理,你应该安装PostgreSQL并将你的开发环境切换到那个.在SQLite之上开发并部署到PostgreSQL(或者在一个数据库上开发并在任何其他数据库之上进行部署)是一个坏主意,会给你带来各种各样的悲伤和困惑.