2013-07-14 29 views
0

我正在将rails应用程序的表单sqlite转换为postgres,以便我可以使用heroku进行部署。我已经安装的Postgres,我跑迁移,但是当我尝试运行一个查询,以找到一个房子里,我得到以下错误相关的所有室友多对一的关联转换为postres

PG::Error: ERROR: operator does not exist: character varying = integer 
LINE 1: SELECT COUNT(*) FROM "mates" WHERE "mates"."house_id" = 1 
                 ^
HINT: No operator matches the given name and argument type(s). You might need to add explicit type casts. 
: SELECT COUNT(*) FROM "mates" WHERE "mates"."house_id" = 1 

此错误源于形成了注册认为你是添加房子后重定向到创建和管理员。这里是查看代码:

Extracted source (around line #4): 

1: <div class="container"> 
2: <div class="row"> 
3:  <div class="span5 offset3"> 
4:   <% if current_house.mates.empty? %> 
5:    <h2>Add an Administrator</h2> 
6:   <% else %> 
7:    <h2>Add a New Housemate</h2> 

感谢您的所有帮助!

+0

[从源码应用转换现有导轨postres]的可能重复(http://stackoverflow.com/questions/17643326/converting-existing-rails-app -postres-from-sqlite) –

回答

0

转到add_house_id_to_mates迁移和变更线3。 之后,请删除20130628212206_change_type_of_house_id_in_house.rb迁移。

从零游程迁移:

rake db:drop db:create db:migrate

+0

20130628212206_change_type_of_house_id_in_house.rb? –

+0

@Davis Aldridge - 他的迁移名称是错误的 – rmagnum2002

0

您已经将table mate中的house_id列定义为字符类型,并且它应该是一个整数。有些关系型数据库原谅了这一点,但PostgreSQL不是。

您应该通过迁移对此进行更正,并检查数据库架构是否存在其他此类错误。

因为有可能为一个字符列包含非数字值,迁移时必须发出SQL语句:从字符串到整数

alter table mates 
alter column house_id 
type integer using cast(house_id as integer); 
+0

当我运行迁移'change_column:mates,:house_id,:integer'时,出现以下错误:'PG :: Error:ERROR:column“house_id”无法自动转换为键入整数 提示:指定USING表达式来执行转换.' – user2136807

+0

@ user2136807:http://stackoverflow.com/a/12605902/479863 –

+0

^这是helpfu l,谢谢 – user2136807