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

Posted

技术标签:

【中文标题】Rails PG::UndefinedTable: 错误: 缺少表的 FROM 子句条目【英文标题】:Rails PG::UndefinedTable: ERROR: missing FROM-clause entry for table 【发布时间】:2015-01-22 06:49:18 【问题描述】:

我有模型

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

【问题讨论】:

这会产生什么 SQL? 哦,看来我找到了解决办法 【参考方案1】:

您添加的唯一内容是where-clause。好像有问题:

where(agency: state: 'active')

...并产生错误的条件,因为在这种情况下,哈希键应表示表名。你应该有这个:

where(agencies: state: 'active')

更新

看到这引起了很多关注,我想我也应该建议一种不同的方式来做同样的事情,稍微更可组合:

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

这个怎么样?它不对模型的表名做任何假设(大部分时间并不重要),并允许您使用 scopes

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

# Somewhere else
merge(Agency.active)

【讨论】:

旁注:一段时间以来,我一直在努力查询别名(不是表名),似乎我可以找到一些有用的用途.. . 哦,我分心了几分钟 :) 我已经找到了解决方案,但是您首先在此处的答案中写道 :) 是的 - 问题就在这里 AND "agencies"."state" = 'active' (不是型号名称, 但表名)

以上是关于Rails PG::UndefinedTable: 错误: 缺少表的 FROM 子句条目的主要内容,如果未能解决你的问题,请参考以下文章

PG::UndefinedTable:错误:关系“active_storage_blob”不存在

PG::UndefinedTable: 错误:缺少表“user_events”的 FROM 子句条目

Heroku / Rails:PG :: Undefined Table:错误“[tablename]”在heroku rails迁移上不存在

PG undefinedtable 错误关系用户不存在

ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "geometry_columns" do

ActiveRecord中的表名损坏错误