gpt4 book ai didi

sql - Rails PG::UndefinedTable:错误:缺少表的 FROM 子句条目

转载 作者:行者123 更新时间:2023-11-29 11:09:21 28 4
gpt4 key购买 nike

我有模型

class Offer < ActiveRecord::Base
belongs_to :agency
end

class Agency < ActiveRecord::Base
has_many :offers
end

当我提出这样的要求时 - 一切正常

@offers = Offer.with_state(:confirmed).
includes(:destination, :cruise_line, :ship).
paginate(per_page: 10, page: params[:page]).decorate

但我只想选择属于active 机构的报价(agencies 表中的 state 列),所以我尝试这样做这个:

@offers = Offer.with_state(:confirmed).
includes(:destination, :cruise_line, :ship).
joins(:agency).
where(agency: {state: 'active'}).
paginate(per_page: 10, page: params[:page]).decorate

完成此操作后,我收到错误 PG::UndefinedTable: ERROR: missing FROM-clause entry for table "agency"。我的代码有什么问题?

查询给了我这个错误和 sql:

PG::UndefinedTable: ERROR: missing FROM-clause entry for table "agency" LINE 1: ...id" WHERE ("offers"."state" IN ('confirmed')) AND "agency"."... ^ : 
SELECT "offers"."id" AS t0_r0, "offers"."name" AS t0_r1, "offers"."destination_id" AS t0_r2, "offers"."cruise_line_id" AS t0_r3, "offers"."ship_id" AS t0_r4, "offers"."departure_date" AS t0_r5, "offers"."departure_port_id" AS t0_r6, "offers"."arrival_date" AS t0_r7, "offers"."arrival_port_id" AS t0_r8, "offers"."flight_price" AS t0_r9, "offers"."bonus" AS t0_r10, "offers"."itinerary" AS t0_r11, "offers"."board_language_id" AS t0_r12, "offers"."agency_landing_page" AS t0_r13, "offers"."benefits" AS t0_r14, "offers"."inner_price" AS t0_r15, "offers"."inner_price_normal" AS t0_r16, "offers"."outer_price" AS t0_r17, "offers"."outer_price_normal" AS t0_r18, "offers"."balcony_price" AS t0_r19, "offers"."balcony_price_normal" AS t0_r20, "offers"."suite_price" AS t0_r21, "offers"."suite_price_normal" AS t0_r22, "offers"."lucky_price" AS t0_r23, "offers"."lucky_price_normal" AS t0_r24, "offers"."valid_from" AS t0_r25, "offers"."valid_till" AS t0_r26, "offers"."created_at" AS t0_r27, "offers"."updated_at" AS t0_r28, "offers"."description" AS t0_r29, "offers"."agency_id" AS t0_r30, "offers"."state" AS t0_r31, "destinations"."id" AS t1_r0, "destinations"."name" AS t1_r1, "destinations"."created_at" AS t1_r2, "destinations"."updated_at" AS t1_r3, "cruise_lines"."id" AS t2_r0, "cruise_lines"."name" AS t2_r1, "cruise_lines"."created_at" AS t2_r2, "cruise_lines"."updated_at" AS t2_r3, "ships"."id" AS t3_r0, "ships"."name" AS t3_r1, "ships"."picture" AS t3_r2, "ships"."cruise_line_id" AS t3_r3, "ships"."created_at" AS t3_r4, "ships"."updated_at" AS t3_r5
FROM "offers"
INNER JOIN "agencies" ON "agencies"."id" = "offers"."agency_id"
LEFT OUTER JOIN "destinations" ON "destinations"."id" = "offers"."destination_id"
LEFT OUTER JOIN "cruise_lines" ON "cruise_lines"."id" = "offers"."cruise_line_id"
LEFT OUTER JOIN "ships" ON "ships"."id" = "offers"."ship_id"
WHERE ("offers"."state" IN ('confirmed')) AND "agency"."state" = 'active'
LIMIT 10 OFFSET 0

最佳答案

错误

ERROR: missing FROM-clause entry for table "agency"

...应该暗示您在查询中的某处错误地使用了 agency 作为表名,而没有将其复数化。但是你到底在哪里做的?

你的工作和非工作片段之间的唯一区别是这些位:

joins(:agency).
where(agency: {state: 'active'}).

…a-这两行都指的是agency。好吧,没有捷径,需要检查两者。

joins 部分负责生成查询的 INNER JOIN 部分,如果您查看生成的 SQL,它仅使用 agencies,这是表的实际名称。这是因为 #joins accepts a symbol that denotes an association being joined — Rails 将其追踪到一个 belongs_to 关联,由于您遵守 Rails 的命名约定,该关联指向知道其表名的正确模型,从而使其工作。

然而,where 部分被破坏了。您使用的这个特殊的“子哈希”条件形式expects the hash key to be a table name并直接在查询中使用它。将其替换为表的实际名称,您应该没问题:

where(agencies: {state: 'active'})

#merge

的方式

还有另一种方法可以完成同样的壮举,它涉及到 #merge method, which merges the conditions of one relation into conditions of another 而不是 #where :

merge( Agency.where(state: 'active') )

即使两者是不同模型上的关系,这也确实有效,它可以方便地过滤连接的记录,这正是您正在做的。

此外,它允许您使用模型类的一些功能。

特别是范围:

# Inside Agency
scope :active, -> { where(state: 'active') }

# Somewhere else
merge(Agency.active)

此外,了解其他模型的表名是什么。在 #where 中,您必须在查询中 指定表名,这可能会从外部进入模型的数据库持久性范围。使用 #merge,您可以遵从模型,希望它是唯一的真实来源。

关于sql - Rails PG::UndefinedTable:错误:缺少表的 FROM 子句条目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27106853/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com