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

Invalid SQL when Nested after Query and Embedded is Used #1632

Closed
rodrigopalhares opened this issue Sep 20, 2019 · 5 comments · Fixed by #1637
Closed

Invalid SQL when Nested after Query and Embedded is Used #1632

rodrigopalhares opened this issue Sep 20, 2019 · 5 comments · Fixed by #1637

Comments

@rodrigopalhares
Copy link

Quill generate a wrong query when referencing a EMBEDED entity in a NEST entity.

Version: 3.4.8
Module: quill-core
Database: postgres

case class MyEmb(name: String) extends Embedded
case class MyParent(myEmb: MyEmb)

query[MyParent]
  .nested
  .filter(_.myEmb.name == "bug")

Expected behavior

SELECT x1.my_embname FROM (SELECT x.name AS myEmbname FROM my_parents x) AS x1 WHERE x1.myEmbname = 'bug'

Actual behavior

SELECT x1.my_embname FROM (SELECT x.name AS myEmbname FROM my_parents x) AS x1 WHERE x1.name = 'bug'

Workaround

@getquill/maintainers

@deusaquilus
Copy link
Collaborator

deusaquilus commented Sep 22, 2019

Whoops, what I was saying before was wrong. This does seem to work for distinct though:

    val q = quote {
      query[MyParent]
        .distinct
        .filter(_.myEmb.name == "bug")
    }
// SELECT x1.myEmbname FROM (SELECT DISTINCT x1.name AS myEmbname FROM MyParent x1 WHERE x1.name = 'bug') AS x1

@rodrigopalhares
Do you have a reasonable workaround for now?
Also, I'm curious to know why the nested is needed in this case? Is there a top-level query that won't work without it?

@deusaquilus deusaquilus changed the title Invalid query in NEST query with EMBEDED entity Invalid SQL when Nested Immediately after Query and Embedded is Used Sep 22, 2019
@deusaquilus deusaquilus changed the title Invalid SQL when Nested Immediately after Query and Embedded is Used Invalid SQL when Nested after Query and Embedded is Used Sep 23, 2019
@cemelo
Copy link

cemelo commented Sep 23, 2019

@deusaquilus currently, manually setting a SchemaMeta[MyParent] specifying the name of the column worked for us.

Regarding the use case, we have some complex sub queries (that use GROUP BY and window functions) that we need to nest in order to join the result with other tables.

@rodrigopalhares
Copy link
Author

rodrigopalhares commented Sep 23, 2019

This is a simplified version of the real query.

Workaround add implicit schemaMeta for normal query:

implicit val parentSchema = schemaMeta[MyParent]("my_parents") 

run {
  query[MyParent]
    .nested
    .filter(_.myEmb.name == "bug")
}

Or alias to dynamic query:

run {
dynamicQuerySchema[MyParent]("my_parents",
  alias(_.myEmb.name, "name")
)
  .nested
  .filter(_.myEmb.name == "bug")
}

Live workaround

@deusaquilus
Copy link
Collaborator

deusaquilus commented Sep 23, 2019 via email

@cemelo
Copy link

cemelo commented Sep 23, 2019

Thank you for looking into it so quickly. :-)

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 a pull request may close this issue.

3 participants