修正错误有点困难.

所有这些都可以在本地机器上运行.

以下是日志(log):

  ←[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)

不确定错误发生在哪里,以及如何修复.

comments .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 

显示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要严格一些.

您可以try 添加迁移来修复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)中起作用,只要您的数据库中没有任何损坏的数据.

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

Postgresql相关问答推荐

从子查询中的排序结果中获取前X行

PostgreSQL(container)中使用80K子分区表处理共享内存不足错误

如何在PostgreSQL中正确删除数据库

对 VOLATILE 函数的调用会 destruct SELECT 语句的原子性

Postgres 查询指向国外数据工作者的分区表比直接查询 fdw 慢很多倍

是否可以调整 PostgreSQL 中的数组以适应 IN 运算符?

postgres 中的模式前缀,被调用元素的范围

我可以使用 Rails 将数组存储在 hstore 中吗

计算 PostgreSQL 中给定 GPS 坐标的日出和日落时间

如何从 WSL 连接到 windows postgres 数据库

在查询中的复合外键/主键列上连接表

从局域网访问 PostgreSQL 服务器

在 PL/pgSQL 中声明行类型变量

没有查询要创建的in ... error

如何只允许一个表的一行?

从长时间的postgres转换日期

如何从 CSV 为 PostgreSQL 副本生成模式

使用 SQLAlchemy 进行 PostgreSQL ILIKE 查询

Heroku 上的 Postgres 并将单个表转储到转储文件

错误:无法在只读事务中执行 CREATE TABLE