gpt4 book ai didi

sql - rails,SQL,单表继承,表地址为 "subclass"而不是类

转载 作者:行者123 更新时间:2023-11-29 13:34:50 24 4
gpt4 key购买 nike

我有以下设置,模型客户端是模型联系人的子类。 STI 表是联系人。

然后在测试代码中调用时出现错误

it "should have the right clients in the right order" do
@producer.clients.should == [a_client, b_client]
end

SQL 语句错误地寻址了 ORDER BY 子句中不存在的客户表:

1) Producer clients associations should have the right clients in the right order
Failure/Error: @producer.clients.should == [a_client, b_client]
ActiveRecord::StatementInvalid:
PGError: ERROR: missing FROM-clause entry for table "clients"
LINE 1: ...lient') AND "contacts"."producer_id" = 6 ORDER BY clients.na...
^
: SELECT "contacts".* FROM "contacts" WHERE "contacts"."type" IN ('Client') AND "contacts"."producer_id" = 6 ORDER BY clients.name DESC

我不是一个SQL专家,但是如果clients表是寻址的,应该设置某种别名,或者该表应该用它的实名联系人来寻址。

降序来自Client中的default_scope顺序

class Client < Contact
...
default_scope order: 'clients.name DESC'
end

这是在 rails 3.2.11 和 Postgres 9.1 上,pg gem 0.12.2。

最佳答案

我自己发现的:

默认范围应引用 contacts.name,因为它似乎是按原样转发到数据库的 SQL 语句的一部分。

关于sql - rails,SQL,单表继承,表地址为 "subclass"而不是类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15366910/

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