Rails 3 与 PostgreSQL:“update_all”问题在连接表中
在 Rails 3 中,我尝试使用类似 self.children.joins(:parent).where(...).update_all(...)
的语句来选择记录子集并全部更新。这适用于 MySQL,但不适用于 PostgreSQL。 Rails 语法是否错误?
详情
成员属于家庭,家庭有很多成员。所涉及的语句位于 Family 实例方法中,因此“self”是一个家庭:
self.members.joins(:family).
where('spouse_id > 0 OR child OR members.id = families.head_id').
update_all("members.residence_location_id = #{self.residence_location_id}")
换句话说,对于该家庭的成员子集,将家庭resident_location 复制到成员residence_location。
这在 MySQL 中运行得非常好。然而,当我更改为 PostgreSQL 时,我收到此错误:
PGError: ERROR: syntax error at or near "INNER"
LINE 1: UPDATE "members" INNER JOIN "families" ON "families"."id" = ...
UPDATE "members" INNER JOIN "families" ON "families"."id" = "members"."family_id"
SET members.residence_location_id = 27
WHERE ("members".family_id = 425) AND
(spouse_id > 0 OR child OR members.id = families.head_id)
我的 Rails 语句是否不正确,但恰好在 MySQL 中工作?有没有办法修复它以与 PostgreSQL 一起使用?或者也许是一种使用 MetaWhere 的方法?
In Rails 3, I'm trying to use a statement like self.children.joins(:parent).where(...).update_all(...)
to select a subset of records and update them all. This worked using MySQL but is not working with PostgreSQL. Is the Rails syntax wrong?
Details
Members belong to families, families have many members. The statement in question is in a Family instance method, so "self" is a family:
self.members.joins(:family).
where('spouse_id > 0 OR child OR members.id = families.head_id').
update_all("members.residence_location_id = #{self.residence_location_id}")
In other words, for a subset of members of this family, copy the family residence_location to the member residence_location.
This worked perfectly well in MySQL. When I changed to PostgreSQL, however, I get this error:
PGError: ERROR: syntax error at or near "INNER"
LINE 1: UPDATE "members" INNER JOIN "families" ON "families"."id" = ...
UPDATE "members" INNER JOIN "families" ON "families"."id" = "members"."family_id"
SET members.residence_location_id = 27
WHERE ("members".family_id = 425) AND
(spouse_id > 0 OR child OR members.id = families.head_id)
Is my Rails statement incorrect but just happened to work in MySQL? Is there a way to fix it to work with PostgreSQL? Or perhaps a way to use MetaWhere?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅供参考,这个问题可能是由此引起的,现已在 Rails 3.1 中解决,问题: https://github .com/rails/rails/issues/522
FYI, this issue was probably caused by this, now resolved in Rails 3.1, issue: https://github.com/rails/rails/issues/522