gpt4 book ai didi

sql - 对表的 FROM 子句条目的引用无效

转载 作者:行者123 更新时间:2023-11-29 12:20:07 25 4
gpt4 key购买 nike

当我想在列表中搜索参数和顺序时出现此错误:

ERROR: invalid reference to FROM-clause entry for table "customers"

HINT: Perhaps you meant to reference the table alias "customers_contract_periods".

这是我在 rails 4.2.1 中的查询:在我这样做之前:

order = "CONCAT(customers.company_name, customers.last_name)"

和:

contract_periods = ContractPeriod.current
.joins( :contract, :customer )
.merge( Contract.search params[ :search ] )
.select( 'contract_periods.*' )
.where( filter_params )
.reorder( order, :start_on )

和:

contract_periods = contract_periods.merge ContractPeriod.by_state params[ :by_state ] if params[ :by_state ]

contract_periods = contract_periods.paginate( page: params[ :page ],
per_page: params[ :per_page ])


contracts = Contract.includes( :contract_type, :customer )
.where( id: contract_periods.map(&:contract_id) )

contract_types = contracts.map( &:contract_type ).uniq
customers = contracts.map( &:customer )

模型关联:

  class Customer < ActiveRecord::Base

## Associations
belongs_to :billing_address, class_name: 'Address'
has_many :contacts, as: :contactable

has_many :contracts

class Contract < ActiveRecord::Base

belongs_to :contract_type
belongs_to :customer
has_many :contract_periods

class ContractPeriod < ActiveRecord::Base

## Associations
belongs_to :contract
has_one :customer, through: :contract

最佳答案

每当您在查询中引用任何关联表时,您都必须在代码末尾将其作为 .references(:contracts) 提及。

关于sql - 对表的 FROM 子句条目的引用无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30750960/

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