我基本上有以下代码:
ModelA.join(:modelB).update_all('modelA.column = modelB.column')
失败了:
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: missing FROM-clause entry for table "modelB"
LINE 1: ..." SET modelAs.column = modelBs...
^
UPDATE "modelAs" SET modelAs.column = modelBs.column WHERE "modelAs"."id" IN (
SELECT "modelAs"."id" FROM "modelAs" INNER JOIN "modelBs" ON "modelBs"."id" = "modelAs"."modelB_id"
)
查看生成的sql请求,很明显Postgresql希望modelB表出现在查询的更新部分中。它有办法做到这一点还是一个错误?
(问题特定于Postgresql / Postgresql rails适配器。上面的代码适用于MySQL,连接子句也复制在请求的更新部分)