Ruby on rails 多对一关联转换为postres
我正在将rails应用程序表单sqlite转换为postgres,以便可以使用heroku进行部署。我安装了postgres并运行了迁移,但是当我试图运行查询以查找与房子相关的所有室友时,我得到了以下错误Ruby on rails 多对一关联转换为postres,ruby-on-rails,postgresql,Ruby On Rails,Postgresql,我正在将rails应用程序表单sqlite转换为postgres,以便可以使用heroku进行部署。我安装了postgres并运行了迁移,但是当我试图运行查询以查找与房子相关的所有室友时,我得到了以下错误 PG::Error: ERROR: operator does not exist: character varying = integer LINE 1: SELECT COUNT(*) FROM "mates" WHERE "mates"."house_id" = 1
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>
提取的源代码(第4行附近):
1:
2:
三:
4:
5:添加管理员
6:
7:添加新室友
谢谢你的帮助 您已经将表mates中的house_id列定义为字符类型,它应该是整数。一些RDBMS可以原谅这一点,但PostgreSQL却不能 您应该通过迁移来纠正这一点,并检查数据库模式是否存在其他此类错误 由于字符列可能包含非数值,因此迁移时必须发出SQL语句:
alter table mates
alter column house_id
type integer using cast(house_id as integer);
转到
add_house_id_to_mates
migration并将第3行从字符串更改为整数。
在此之后,删除\u house.rb中的\u house\u id\u的
201306282122006\u change\u type\u
从零开始运行迁移:
rake db:drop db:create db:migrate
运行迁移时的可能重复change\u列:mates,:house\u id,:integer
我得到以下错误:PG::error:error:column“house\u id”无法自动强制转换为类型整数提示:指定一个使用表达式来执行转换。
20130628212206\u change\u type\u of_house\u id\u in_house.rb?