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

Generated query checks equality on wrong table #239

Open
jdsalchow opened this issue Jan 28, 2023 · 1 comment
Open

Generated query checks equality on wrong table #239

jdsalchow opened this issue Jan 28, 2023 · 1 comment

Comments

@jdsalchow
Copy link

Version: I've tried 4.0.0 - 4.6.0
Module: protoquill
Database: I've tried postgres and h2

Expected behavior

Given

case class Foo(id: Int, someField: String)
case class Bar(fooId: Int, otherField: String)

I expect

  run(
    (for {
      foo <- query[Foo]
      bar <- query[Bar].join(bar => bar.fooId == foo.id)
    } yield (foo, bar))
      .filter { case (foo, bar) =>
        foo.someField == "baz" && bar.otherField == "boo"
      }
  )

to generate

SELECT foo.id, foo.some_field AS someField, bar.foo_id AS fooId, bar.other_field AS otherField
FROM foo foo
         INNER JOIN bar bar ON bar.foo_id = foo.id
WHERE foo.some_field = 'baz'
  AND bar.other_field = 'boo'

Actual behavior

Instead of foo.some_field = 'baz' the first term in the where clause is bar.some_field = 'baz', i.e. the generated sql is

SELECT foo.id, foo.some_field AS someField, bar.foo_id AS fooId, bar.other_field AS otherField
FROM foo foo
         INNER JOIN bar bar ON bar.foo_id = foo.id
WHERE bar.some_field = 'baz'
  AND bar.other_field = 'boo'

Steps to reproduce the behavior

See https://scastie.scala-lang.org/zYJwK8KoSH2hxV0tmTHLLQ as an example.

Workaround

Rewrite as

  run(
    query[Foo]
      .join(query[Bar])
      .on((foo, bar) => bar.fooId == foo.id)
      .filter { case (foo, bar) =>
        foo.someField == "baz" && bar.otherField == "boo"
      }
  )

@getquill/maintainers

@joelsonoda
Copy link

@jdsalchow, this appears to also exist in zio-quill and sounds like it could be related to zio/zio-quill#2671

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

No branches or pull requests

2 participants