简体   繁体   English

PGSQL-to_tsquery和JOINS导致Rails中的错误

[英]PgSQL - to_tsquery and JOINS leads to Error in Rails

I have the following query: 我有以下查询:

query = param.tag_list.join("|")

title = "ts_headline(title, query) AS title" 
rank = "ts_rank_cd(tsv, query) AS rank"

Job.select(
  [
    title, rank, :starts_at, :slug, :job_position_id, :id, :city_id,
    :company_office_id
  ]
).from(
  "jobs, to_tsquery('english', '#{query}') AS query"
).where(
  "tsv @@ query AND enabled = true"
).with_position.with_city.with_office.order(
  "rank DESC, starts_at DESC"
).limit(limit)

The with_* scopes just do an includes on positions, city, office. with_ *范围仅包含职位,城市,办公室的内容。 Not more. 不多。

This leads to Arel making a query that produces an error: 这导致Arel进行查询并产生错误:

ActiveRecord::StatementInvalid - PG::Error: ERROR: invalid reference to FROM-clause entry for table "jobs" LINE 1: ...ER JOIN "job_positions" ON "job_positions"."id" = "jobs"."jo... ^ HINT: There is an entry for table "jobs", but it cannot be referenced from this part of the query. ActiveRecord :: StatementInvalid-PG :: Error:错误:对表“ jobs”的FROM子句条目的引用无效第1行:... ER JOIN“ job_positions” ON“ job_positions”。“ id” =“ jobs”。“ jo ... ^提示:表“ jobs”有一个条目,但是不能从查询的这一部分中引用它。

This is most likely related to the problem of mixing implicit and explicit JOINS. 这很可能与混合隐式和显式JOINS的问题有关。 See also this ERROR: invalid reference to FROM-clause 另请参见此错误:对FROM子句的无效引用

The thing is that I don't know how to rewrite the query with the to_tsquery so that it works? 问题是我不知道如何使用to_tsquery重写查询,以便它能正常工作?

Muchas gracias in advance for any idea 任何想法提前Muchas gracias

UPDATE 更新

Here the exact query that is produced by Arel in the end. 最后,由Arel产生的确切查询。 The error refers to the jobs.job_position_id from the first LEFT OUTER JOIN 错误是指第一个LEFT OUTER JOIN中的jobs.job_position_id

SELECT  
    ...
FROM 
    jobs, to_tsquery('english', 'coffeescript|backbone.js|javascript|node.js') AS query 
LEFT OUTER JOIN 
    "job_positions" ON "job_positions"."id" = "jobs"."job_position_id" 
LEFT OUTER JOIN 
    "cities" ON "cities"."id" = "jobs"."city_id" 
LEFT OUTER JOIN 
    "company_offices" ON "company_offices"."id" = "jobs"."company_office_id" 
LEFT OUTER JOIN 
    "companies" ON "companies"."id" = "company_offices"."company_id" 
WHERE 
    (tsv @@ query AND enabled = true) 
ORDER BY 
    rank DESC, starts_at DESC 
LIMIT 9

It's a bit hard to tell where the error is coming from exactly without the SQL you're actually generating, but you're almost certainly referencing jobs in a subquery that is at the same level as jobs in the query tree. 很难确切地知道错误的出处是什么,而实际上没有实际生成的SQL,但是几乎可以肯定的是,您在引用的子查询中的作业与查询树中的作业处于同一级别。

Basically, the construct that won't work is the following: 基本上,无效的构造如下:

select ...
from foo, (select ... from bar where bar.x = foo.y) as baz

The easiest fix usually is to alias things accordingly: 最简单的解决方法通常是相应地为事物添加别名:

select ...
from foo, (select ... from bar, foo as foo2 where bar.x = foo2.y and ...) as baz

See this question here for another example: 参见以下示例的问题:

SQL subquery questions, "ERROR: invalid reference to FROM-clause entry ..." SQL子查询问题,“错误:对FROM子句条目的引用无效……”


In your case, this: 就您而言,这是:

FROM 
    jobs, to_tsquery('english', 'coffeescript|backbone.js|javascript|node.js') AS query 
WHERE 
    (tsv @@ query AND enabled = true) 

Should be: 应该:

FROM 
    jobs
WHERE 
    (tsv @@ to_tsquery('english', 'coffeescript|backbone.js|javascript|node.js') AND enabled = true)

This should work too: 这也应该工作:

FROM 
    ( select to_tsquery('english', 'coffeescript|backbone.js|javascript|node.js') AS query ) q,
    jobs 
WHERE 
    (tsv @@ q.query AND enabled = true) 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM