drizzle-orm
drizzle-orm copied to clipboard
[FEATURE]: Auto-Alias Columns in Subqueries
Describe what you want
Feature Request: Automatically Alias Columns in Subqueries to Avoid Ambiguity
Issue:
When using a custom select in a query that involves joins, and the query is used as a subquery with .select() (select all), an error like column reference "column_name" is ambiguous occurs. This is evident when inspecting the generated query via .toSQL().
Example: Consider the following TypeScript code:
const tableOneDefaultSelect = {
created_at: tableOne.createdAt,
updated_at: tableOne.updatedAt,
deleted_at: tableOne.deletedAt,
created_by: tableOne.createdBy,
updated_by: tableOne.updatedBy,
deleted_by: tableOne.deletedBy,
id: tableOne.id,
key: tableOne.key,
// ...
};
const tableTwoDefaultSelect = { id: tableTwo.id, title: tableTwo.title };
const tableThreeDefaultSelect = { /* similar structure */ };
const tableFourDefaultSelect = { /* similar structure */ };
const baseCriteria = and(
isNull(tableOne.deletedAt),
eq(tableFour.key, "published"),
// ...
);
const aliasTableThree = alias(tableThree, "table_boo");
const baseQuery = db
.select({
...tableOneDefaultSelect,
table_two: tableTwoDefaultSelect,
table_three: {
...tableThreeDefaultSelect,
name: tableThree.title,
},
table_four: tableFourDefaultSelect,
})
.from(tableOne)
.leftJoin(tableTwo, eq(tableTwo.id, tableOne.tableTwoId))
.leftJoin(tableThree, eq(tableThree.id, tableOne.tableThreeId))
.leftJoin(tableFour, eq(tableFour.id, tableOne.tableFourId))
.leftJoin(tableOneTableAce, eq(tableOneTableAce.tableOneId, tableOne.id)) // Junction table
.leftJoin(tableAce, eq(tableAce.id, tableOneTableAce.tableAceId))
.leftJoin(tableOneTableBoo, eq(tableOneTableBoo.tableOneId, tableOne.id)) // Junction table
.leftJoin(aliasTableThree, eq(aliasTableThree.id, tableOneTableBoo.tableThreeId))
.orderBy(desc(tableOne.updatedAt))
.where(baseCriteria);
const finalQuery = await db
.select()
.from(baseQuery)
.where(...);
The generated SQL query from db.select().from(baseQuery).where(...).toSQL() shows the following (formatted for readability):
select
"created_at", "updated_at", "deleted_at", "id", "key", /* ... */,
"id", "key", "title", "id", "title", "type", "is_active", "id", "key", "title"
from
(
select
"table_one"."created_at", "table_one"."updated_at", "table_one"."id", "table_one"."key", /* ... */,
"table_two"."id", "table_two"."key", "table_two"."title",
"table_three"."id", "table_three"."title", "table_three"."type", "table_three"."is_active",
"table_four"."id", "table_four"."key", "table_four"."title"
from
"table_one"
left join "table_two" on "table_two"."id" = "table_one"."table_two_id"
left join "table_three" on "table_three"."id" = "table_one"."table_three_id"
left join "table_four" on "table_four"."id" = "table_one"."table_four_id"
/* ... */
where
"table_one"."deleted_at" is null
and "table_four"."key" = $1
/* ... */
order by
"table_one"."updated_at" desc
) "documents"
where
/* ... */
The resulting SQL contains multiple ambiguous column names (id, key, title), leading to the error.
Proposed Solution:
If possible, enhance the query builder to automatically alias columns in subqueries when using .select() to avoid ambiguity. The alias could follow a pattern like "table_name.column_name" or "table_name$column_name", so the result types remain correctly mapped in TypeScript by drizzle.
Expected SQL Output: The inner query would automatically alias columns as follows:
select
"table_one.created_at", "table_one.updated_at", /* ... */,
"table_two.id", "table_two.title",
"table_three.id", "table_three.title",
/* ... */
from
(
select
"table_one"."created_at" as "table_one.created_at",
"table_one"."updated_at" as "table_one.updated_at",
/* ... */
"table_two"."id" as "table_two.id",
"table_two"."title" as "table_two.title",
/* ... */
from
"table_one"
left join "table_two" on "table_two"."id" = "table_one"."table_two_id"
left join "table_three" on "table_three"."id" = "table_one"."table_three_id"
/* ... */
) "documents"
where
/* ... */
This is seemingly the same issue that was presented in #1242. That issue was closed by it's OP but the issue seems to persist for others including my team, I was going to relog it but the noticed this ticket.
From reading through #1242 it seems like #1703 was opened a while back to address this but that PR seems to have gone stale. Perhaps @Angelelz's or someone from the drizzle team can shed some light on why the PR has gone stale?
I didn't realized there's already an issue, I guess because it's already closed way before I face this problem, so no wonder I couldn't find it (I filter by open issue)
I'm sorry for the double mention but maybe @Angelelz or someone from the drizzle team can give us an update if the PR will be going through or not?
I am facing the same issue.
I have Duplicate column name 'ID error in a similar context.
I have special wrapper for pagination that uses data query as subquery for calculating the total count for pagination. But this query has lots of joins, some of them sharing keys. Running the query as it is, produces an expected result. But for some reason, when I run it as subquery, it throws the error.
const baseQuery = db.select({
expense: splitExpenseDb,
paidBy: userDb,
paidByAvatar: userAvatarDb
}).from(splitExpenseDb)
...{joins, where, etc}
.$dynamic()
const { total, data } = await withPagination(db, baseQuery, pagination)
And pagination:
db.select({ count: count() }).from(baseQuery.as('sq'))
Which produces this query:
select count(*)
from (select {...other fields}, `user`.`ID`, `user_avatar`.`ID`, // <- conflicting fields
left join `user_avatar` on `user_avatar`.`UID` = `user`.`avatar_UID`
where `split_transaction`.`entity_id` = 2
order by CASE WHEN status = 'draft' THEN 0 ELSE 1 END, `split_transaction`.`created_at` desc, `split_transaction`.`id` desc) `sq`
So fields user.ID and user_avatar.ID conflict each, even though having this query alone (not as subquery) works fine.
Don't know if some kind of auto-aliasing would help here.
Hey everyone!
I've created this message to send in a batch to all opened issues we have, just because there are a lot of them and I want to update all of you with our current work, why issues are not responded to, and the amount of work that has been done by our team over ~8 months.
I saw a lot of issues with suggestions on how to fix something while we were not responding – so thanks everyone. Also, thanks to everyone patiently waiting for a response from us and continuing to use Drizzle!
We currently have 4 major branches with a lot of work done. Each branch was handled by different devs and teams to make sure we could make all the changes in parallel.
First branch is drizzle-kit rewrite
All of the work can be found on the alternation-engine branch. Here is a PR with the work done: https://github.com/drizzle-team/drizzle-orm/pull/4439
As you can see, it has 167k added lines of code and 67k removed, which means we've completely rewritten the drizzle-kit alternation engine, the way we handle diffs for each dialect, together with expanding our test suite from 600 tests to ~9k test units for all different types of actions you can do with kit. More importantly, we changed the migration folder structure and made commutative migrations, so you won't face complex conflicts on migrations when working in a team.
What's left here:
- We are finishing handling defaults for Postgres, the last being geometry (yes, we fixed the
sridissue here as well). - We are finishing commutative migrations for all dialects.
- We are finishing up the command, so the migration flow will be as simple as
drizzle-kit upfor you.
Where it brings us:
- We are getting drizzle-kit into a new good shape where we can call it
[email protected]!
Timeline:
- We need ~2 weeks to finish all of the above and send this branch to beta for testing.
Second big branch is a complex one with several HUGE updates
- Bringing Relational Queries v2 finally live. We've done a lot of work here to actually make it faster than RQBv1 and much better from a DX point of view. But in implementing it, we had to make another big rewrite, so we completely rewrote the drizzle-orm type system, which made it much simpler and improved type performance by ~21.4x:
(types instantiations for 3300 lines production drizzle schema + 990 lines relations)
TS v5.8.3: 728.8k -> 34.1k
TS v5.9.2: 553.7k -> 25.4k
You can read more about it here.
What's left here:
- We have 1 issue with TS that is already in progress of being fixed. The issue and Post about fixing.
Where it brings us:
- We are getting drizzle-orm into a new good shape where we can call it
[email protected]!
Breaking changes:
- We will have them, but we will have open channels for everyone building on top of drizzle types, so we can guide you through all the changes.
Third branch is adding support for CockroachDB and MSSQL dialects
Support for them is already in the alternation-engine branch and will be available together with the drizzle-kit rewrite.
Summary
All of the work we are doing is crucial and should be done sooner rather than later. We've received a lot of feedback and worked really hard to find the best strategies and decisions for API, DX, architecture, etc., so we can confidently mark it as v1 and be sure we can improve it and remain flexible for all the features you are asking for, while becoming even better for everyone building on top of the drizzle API as well.
We didn't want to stay with some legacy decisions and solutions we had, and instead wanted to shape Drizzle in a way that will be best looking ahead to 2025–2026 trends (v1 will get proper effect support, etc.).
We believe that all of the effort we've put in will boost Drizzle and benefit everyone using it.
Thanks everyone, as we said, we are here to stay for a long time to build a great tool together!
Timelines
We are hoping to get v1 for drizzle in beta this fall and same timeline for latest. Right after that we can go through all of the issues and PRs and resond everyone. v1 for drizzle should close ~70% of all the bug tickets we have, so on beta release we will start marking them as closed!
No offense @AndriiSherman , but I am really annoyed by this message. I received it like 8 times for different issues, where usually really useful comments pop up. So now everyone subscribed to one of those, received your message, which in all my cases says nothing more than "FYI we're working on something entirely different" – which is fine by me, but doesn't require your bulk messaging. For me, it is the maintainer equivalent of "+1".