Remove alias in Slick generated queries

2019-08-03 09:44发布

问题:

I am using Slick for database operations in my application (using Postgres as the database). I am facing some issues while trying to build generalized queries. I have asked a question in SO for using the table name instead of alias (here), but found out that its no possible.

So I am now trying to fix the issue in the Slick side.

def ++= (selectStatement:String) ={
    this.customSelectStatement = Some(selectStatement + " E where " +  getQuery(filterPropertyList))
  }

filterQuery ++= (TableQuery[SkillTable] innerJoin TableQuery[SkillCategoryTable] on (_.skillCategoryId === _.id)).sortBy(_._1.id).map{ case (a,at) => (a.id,a.name,at.name)}.selectStatement
    val result = filter[SkillContainer](filterQuery)

The method getQuery build a SQL where condition query based on the properties it receives. I am building the select clause using the Slick expressions and then converting it to SQL query and then appending the where condition built through getQuery.

When I run the code, the query is getting generated as below:

SELECT x2.x3,
       x2.x4,
       x5.x6
FROM (
  SELECT x7."SkillId" AS x3,
         x7."Name" AS x4,
         x7."Description" AS x8,
         x7."IsRemoved" AS x9,
         x7."SkillCategoryId" AS x10
  FROM "base"."Skill" x7
) x2
  INNER JOIN (
    SELECT x11."SkillCategoryId" AS x12,
           x11."Name" AS x6,
           x11."Description" AS x13,
           x11."IsRemoved" AS x14
    FROM "base"."SkillCategory" x11
  ) x5 ON x2.x10 = x5.x12
WHERE base."Skill"."IsRemoved" = 'false' 
LIMIT 10 offset 0

But the above query will fail since base."Skill"."IsRemoved" will not be resolved as I Slick generates the query with aliases. However, if I change it to x2.x9 = 'false', then the query executes successfully.

The where condition query is built by getQuery method, and it doesn't know the aliases to be used. Is there any way in which I can ask Slick not to generate aliases for columns and also provide the sub query alias(x2) in the expression itself, so that I will have control over the alias of the sub query.?

回答1:

The "maybefilter" probably allows you to do this with the type-safe Slick api without concatenating SQL strings. https://gist.github.com/cvogt/9193220

Other than that, removing the aliases, when possible would be a nice improvement of the query compiler, which I have though of in the past sometimes, too. Please add a ticket: https://github.com/slick/slick/issues