Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Nested query in select clause is not wrapped in paranthesis #104

Closed
wassertim opened this Issue · 9 comments

3 participants

@wassertim

Using MySql driver and writing this:

  def subQuery(c: CategoriesTable.type) = (for {
    p <- ProductsTable
  } yield(p.id.count))
  for {
    c <- CategoriesTable
    if (c.parentId === 2)
  } yield(c.id, c.title, (subQuery(c).asColumn))

I expect something like this:

 select 
    x2.categoryId, 
    x2.title, 
    (select count(x3.productId) from products x3) as c1
 from 
    categories x2 
 where x2.parentCategoryId = 2

But I get this:

  select 
    x2.categoryId, 
    x2.title, 
    select count(x3.productId) from products x3 
  from 
    categories x2 
  where x2.parentCategoryId = 2

which is invalid in MySql

@EECOLOR

I have made an attempt at a solution here: http://stackoverflow.com/questions/14920153/how-to-write-nested-queries-in-select-clause.

I think it's a good candidate to support out of the box as it's usable for most SQL drivers.

@wassertim

I wonder why it has got so little attention.

@szeiger
Owner

Compiling this code should produce two warnings for:

@deprecated("asColumn subverts correct typing and can lead to incorrect queries", "1.0.0")
def asColumn: Column[P1] = {

@deprecated("Use Query.count instead", "1.0")
def count = Library.Count.column[Int](n)

And an incorrect query is exactly what you get here. Both methods will be removed in Slick 2.0. Using the recommended solution (c.length instead of subQuery(c).asColumn) produces correct SQL code.

@szeiger szeiger closed this
@wassertim

If I use length I get more weird results.

@szeiger
Owner

As in #101 ? I'm just investigating those.

@wassertim

No it's not similar. You should try it with MySql driver as I explained. Try to use length, count or whatever - you don't get the expected result.

@wassertim

I use the EECOLOR's workaround. Take a look there too.

@szeiger
Owner

Please provide a standalone snippet to reproduce this. I do get the expected result with my attempts.

@szeiger
Owner

It turned out that #101 (double select keyword) is actually the same symptom as this issue (missing parentheses). If there is any remaining issue when using Query.length, it should be caught by the fix to #101.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.