问题描述
修正一个错误有一点小麻烦。所有在本地机器上都能很好地工作。
在PG上,heroku是错误的。
以下是日志:
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m ActionView :: Template :: Error(PGEr
ror:ERROR:operator does不存在:character varying =整数
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m LINE 1:... Treviews。* FROM re
viewsWHEREreviews。trip_id= 32
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m:SELECT] 。* FROMreview
sWHEREreviews。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 @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提示:没有操作符匹配gi
ven名称和参数类型。您可能需要添加显式类型转换。
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m app / controllers / trips_controlle
r.rb:21:'show'
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m
←[32m2012-01-09T19:52:24 + 00:00 app [web。 1]:←[0m缓存:[GET / trips / 32] miss
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m 36:< li> ; <%= review.conte
nt%> < /锂>
←[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已完成500内部服务器错误
或86ms
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[ 0m参数:{id=>32}
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m处理方式TripsController#s
如何作为HTML
←[32m2012-01-09T19:52:24 + 00:00 app [web.1]:←[0m在布局中使用
渲染旅行/ show.html.erb /应用程序(81.8ms)
不确定发生错误的位置以及如何解决。 / b>
reviews.rb
class Review< ActiveRecord :: Base
belongs_to:trip
end
$ b $ class Trip< ActiveRecord :: Base
has_many:reviews,:dependent => :销毁
attr_accessible,:reviews_attributes
accept_nested_attributes_for:reviews,:reject_if => lambda {| a |一[:名字] .blank? },:allow_destroy => true
end
show.html.rb
<%= link_to阅读评论,#inline,:id => 'various1',:class => 'review'%>
< div style ='display:none'>
< div id =inline>
< ul>
< li> <%= review.content%> < /锂>
< li> < I> <%= review.name%> < I> /; < /锂>
< / ul>
<%end%>
< / div>
< / div>
令我困惑的是我有两个其他几乎相同的模型,但它们运作良好。
谢谢!
您的问题在这里:
WHEREreviews。trip_id= 32
,错误信息表示:
$ b $因此,您已将 reviews
中的 trip_id
列创建为字符串而不是整数。这在SQLite中可以正常工作,因为SQLite的类型系统相当宽松,但在PostgreSQL中不起作用,因为PostgreSQL要严格一些。
您可以尝试添加迁移修正 trip_id
的类型:
def def
change_column:reviews,:trip_id,:整数
结束
如果不起作用然后删除并重新创建该表:
def change
drop_table:reviews
create_table:reviews do | t |
#...
t.integer:trip_id
#...
end
end
如果您有要保留的数据并且 change_column
doesn,那么您也可以通过原始SQL执行ALTER TABLE 't work:
def change
执行%q {
改变表格评论
改变列trip_id
类型int使用cast(trip_id作为int)
}
结束
只要您的 trip_id
中没有任何损坏的数据,它就可以在PostgreSQL(但不是SQLite)中工作。
一旦你有了解,你应该安装PostgreSQL并将你的开发环境切换到那个版本。在SQLite之上开发并部署到PostgreSQL(或者在一个数据库之上开发并在其他数据库之上部署)是一个不好的主意,并且会给你带来各种各样的悲伤和困惑。
Having a little trouble fixing an error.
All works great on local machine.On PG, heroku is the error.
Here are the logs :
←[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)
Not really sure where exactly, the error occurs and how to fix it.
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>
The thing that confuses me is that I have two other practically the same models, but they work well.
Thanks!
Your problem is here:
WHERE "reviews"."trip_id" = 32
and the error message says that:
so you have created your trip_id
column in reviews
as a string rather than as an integer. That will work fine in SQLite because SQLite's type system is rather loose but it won't work in PostgreSQL as PostgreSQL is quite a bit stricter.
You could try adding a migration to fix the type of trip_id
:
def change
change_column :reviews, :trip_id, :integer
end
and if that doesn't work then drop and recreate the table:
def change
drop_table :reviews
create_table :reviews do |t|
#...
t.integer :trip_id
#...
end
end
You could also do an ALTER TABLE through raw SQL if you have data that you want to preserve and the change_column
doesn't work:
def change
execute %q{
alter table reviews
alter column trip_id
type int using cast(trip_id as int)
}
end
That should work in PostgreSQL (but not SQLite) as long as you don't have any broken data in your trip_id
.
Once you have that sorted out, you should install PostgreSQL and switch your development environment to that. Developing on top of SQLite and deploying to PostgreSQL (or developing on top of one database and deploying on top of any other database for that matter) is a bad idea and will cause you all sorts of grief and confusion.
这篇关于Rails 3.1。 Heroku PGError:运算符不存在:character varying = integer的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!