Remove alias in Slick generated queries

回眸只為那壹抹淺笑 提交于 2019-12-24 15:07:01

问题


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



来源:https://stackoverflow.com/questions/27937827/remove-alias-in-slick-generated-queries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!