SQLite Table alias with period inside parentheses is not found

时间:2016-07-11 22:06:15

标签: sql sqlite

I am working on a open source ORM, and have ended up with the following generated Statement

SELECT "Task"."id"
,"Task"."title"
,"Task"."projectId"
,"Project"."id" AS "Project.id"
,"Project"."title" AS "Project.title"
,"Project"."userId" AS "Project.UserId"
,"Project.User"."id" AS "Project.User.id"
,"Project.User"."username" AS "Project.User.username"
FROM "Task" AS "Task"
LEFT OUTER JOIN  ("Project" AS "Project" 
INNER JOIN "User" AS "Project.User" ON "Project"."userId" = "Project.User"."id"
    AND "Project.User"."username" = 'test01' ) ON "Task"."projectId" = "project"."id";

which produces the following error

no such column: Project.User.id

Thanks to help from a previous question here I was able to solve it by removing the periods from the alias inside the parentheses

SELECT "Task"."id"
,"Task"."title"
,"Task"."projectId"
,"Project"."id" AS "Project.id"
,"Project"."title" AS "Project.title"
,"Project"."userId" AS "Project.UserId"
,"Project_User"."id" AS "Project.User.id"
,"Project_User"."username" AS "Project.User.username"
FROM "Task" AS "Task"
LEFT OUTER JOIN  ("Project" AS "Project" 
INNER JOIN "User" AS "Project_User" ON "Project"."userId" = "Project_User"."id"
    AND "Project_User"."username" = 'test01' ) ON "Task"."projectId" = "project"."id";

However, since I am working on an ORM the format of the the SQL statement is important and having the periods in those inner aliases would help a lot. I have discovered the following SQL works.

SELECT "Task"."id"
,"Task"."title"
,"Task"."projectId"
,"Project"."id" AS "Project.id"
,"Project"."title" AS "Project.title"
,"Project"."userId" AS "Project.UserId"
,"Project.User"."id" AS "Project.User.id"
,"Project.User"."username" AS "Project.User.username"
FROM "Task" AS "Task"
LEFT OUTER JOIN  ("Project" AS "Project" 
INNER JOIN "User" AS "Project.User" ON "Project"."userId" = "Project.User"."id"
    AND "Project.User"."username" = 'test01' ) "Project.User" ON "Task"."projectId" = "project"."id";

But I do not understand why, or how to scale this to a solution that has many nested joins of a similar nature.

All columns and tables exist with the names given, and no other problems are getting in the way (I assume this mainly based on the failure of the first example and the success of the second.) The initial error only happens in sqlite, all over dialects I have tried seem to not have an issue.

My question is, a, why does the third example work and, b, would it scale to even deeper joins with aliases with periods?

0 个答案:

没有答案