Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chained associated against pg_scope and regular scope fail #243

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

doits
Copy link
Contributor

@doits doits commented May 1, 2015

This PR only adds failing tests for now. (New PR based of master in comparison to #241)

Looks like the error has the same symptoms like #235 but a different cause ... here it seems to be the automatic table aliasing done by rails. Let me explain.

If you simply do Person.joins(:houses).to_sql, everything is fine:

Person.joins(:houses).to_sql

SELECT \"with_model_people_41998\".* FROM \"with_model_people_41998\"
INNER JOIN \"with_model_houses_41998\"
ON \"with_model_houses_41998\".\"person_id\" = \"with_model_people_41998\".\"id\"

But if you introduce a second join to the same table, even by a string option, it automatically aliases the first join. (the following query is invalid sql but shows what rails does):

Person.joins(:houses).joins(
"INNER JOIN \"with_model_houses_41998\" ON"
).to_sql

SELECT \"with_model_people_41998\".* FROM \"with_model_people_41998\"
INNER JOIN \"with_model_houses_41998\" \"houses_with_model_people_41998\"
ON \"houses_with_model_people_41998\".\"person_id\" = \"with_model_people_41998\".\"id\"
INNER JOIN \"with_model_houses_41998\" ON

Interesting thing is, that if you change the join string to INNER JOI ... (so only remove the N from JOIN) or remove the last N ("INNER JOIN \"with_model_houses_41998\" O), no alias is created:

Person.joins(:houses).joins(
"INNER JOIN \"with_model_houses_41998\" O"
).to_sql

SELECT \"with_model_people_41998\".* FROM \"with_model_people_41998\"
INNER JOIN \"with_model_houses_41998\"
ON \"with_model_houses_41998\".\"person_id\" = \"with_model_people_41998\".\"id\"
INNER JOIN \"with_model_houses_41998\" O

The problem lies in the fact that this table aliasing is also done when the join is happening in a subquery:

Person.joins(:houses).joins(
"INNER JOIN (select * from something INNER JOIN \"with_model_houses_41998\" ON) table_alias"
).to_sql

SELECT \"with_model_people_41998\".* FROM \"with_model_people_41998\"
INNER JOIN \"with_model_houses_41998\" \"houses_with_model_people_41998\"
ON \"houses_with_model_people_41998\".\"person_id\" = \"with_model_people_41998\".\"id\"
INNER JOIN (select * from something INNER JOIN \"with_model_houses_41998\" ON) table_alias

I'm not sure, but this sub query should not change the outer query and the table aliasing done by rails is unnecessary (or not?).

A solution on the pg_search side is simply to do table aliasing in the subquery by itself:

Person.joins(:houses).joins(
"INNER JOIN (select * from something INNER JOIN \"with_model_houses_41998\" \"pg_table_alias\" ON) table_alias"
).to_sql

SELECT \"with_model_people_41998\".* FROM \"with_model_people_41998\"
INNER JOIN \"with_model_houses_41998\"
ON \"with_model_houses_41998\".\"person_id\" = \"with_model_people_41998\".\"id\"
INNER JOIN (select * from something INNER JOIN \"with_model_houses_41998\" \"pg_table_alias\" ON) table_alias

But this means the join must be done by hand in association.rb or at least I do not know any way to specify a table alias manually in .joins().

So main question are:

  • Is automatic table aliasing done by rails / activerecord because of a join in a subquery unnecessary? If yes, maybe report this to the rails team and they can remove it ...
  • Can pg_search set a table alias manually in the subquery join to stop automatic table aliasing?
  • Or is it much simpler to fix and the problem lies elsewhere?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant