drizzle-orm icon indicating copy to clipboard operation
drizzle-orm copied to clipboard

[BUG]: Aliasing the same table twice and then inner joining one while left joining the other leads to type never

Open iSchoen opened this issue 1 year ago • 4 comments

What version of drizzle-orm are you using?

0.31.1

What version of drizzle-kit are you using?

No response

Describe the Bug

If I make two table aliases for the same table and then inner join one while left joining the other, the return type of my query ends up being never[].

const fromCompany = aliasedTable(company, "fromCompany");
const toCompany = aliasedTable(company, "toCompany");

const data = await db
  .select({
    referral,
    fromCompany,
    toCompany,
  })
  .from(referral)
  .innerJoin(fromCompany, eq(fromCompany.id, referral.fromCompanyId))
  .leftJoin(toCompany, eq(toCompany.id, referral.toCompanyId));

Unfortunately the type of data becomes:

const data: never[]

I've reduced the Company table to the smallest it could possibly be but it makes absolutely no difference:

export const company = mysqlTable(
  "Company", {
    id: int("id").primaryKey().autoincrement(),
  }
);

Interestingly, if both joins to the Company table are left joins or if both joins are inner joins, it works fine, but that's not my intended behavior.

// So this works
const data = await db
  .select({
    referral,
    fromCompany,
    toCompany,
  })
  .from(referral)
  .leftJoin(fromCompany, eq(fromCompany.id, referral.fromCompanyId))
  .leftJoin(toCompany, eq(toCompany.id, referral.toCompanyId));

// And so does this
const data = await db
  .select({
    referral,
    fromCompany,
    toCompany,
  })
  .from(referral)
  .innerJoin(fromCompany, eq(fromCompany.id, referral.fromCompanyId))
  .innerJoin(toCompany, eq(toCompany.id, referral.companyId));

Types aside, my query works as intended at runtime with one inner join and one left join.

Expected behavior

I expect that I can alias the same table twice and be able to inner join one while left joining the other.

Environment & setup

Typescript: 5.4.5

iSchoen avatar Sep 18 '24 21:09 iSchoen

I just encountered the same issue today

lasseklovstad avatar Sep 20 '24 11:09 lasseklovstad

I'm having the same issue.

const player1 = aliasedTable(players, 'player1')
const player2 = aliasedTable(players, 'player2')

return db
  .select({
    id: matches.id,
    player1: player1.name,
    player2: player2.name,
    winnerId: matches.winnerId,
    player1Id: matches.player1Id,
  })
  .from(matches)
  .innerJoin(player1, eq(matches.player1Id, player1.id))
  .leftJoin(player2, eq(matches.player2Id, player2.id))
  .where(eq(matches.roundId, roundId))
Screenshot 2024-09-22 at 11 10 54 PM

leonardorb avatar Sep 23 '24 03:09 leonardorb

I'm having the same issue

gklee555 avatar Sep 24 '24 20:09 gklee555

Try using the alias function instead of aliasedTable alias can be imported from "drizzle-orm/sqlite-core' or MySQL core

Imtiajrex avatar Sep 28 '24 15:09 Imtiajrex

Try using the alias function from instead of aliasedTable alias can be imported from "drizzle-orm/sqlite-core' or MySQL core

that worked for me

lasseklovstad avatar Oct 08 '24 11:10 lasseklovstad

That indeed works. If you're using postgres:

import { alias } from 'drizzle-orm/pg-core'

leonardorb avatar Oct 08 '24 16:10 leonardorb

It does, in fact work! Thanks for the insight everyone!

That is to say, use alias. Do not use aliasedTable.

iSchoen avatar Oct 09 '24 19:10 iSchoen

aliasedTable is only for use in the RQB API. I see that the docs uses that function instead of alias: https://orm.drizzle.team/docs/joins#aliases--selfjoins.

We'll fix this documentation issue.

L-Mario564 avatar Oct 24 '24 18:10 L-Mario564

Fixing docs now, sorry for late response here

AndriiSherman avatar Jan 29 '25 17:01 AndriiSherman

Fixing docs now, sorry for late response here

Is it fixed on docs? I'm still seeing the old guidance. https://orm.drizzle.team/docs/joins#aliases--selfjoins

MatthewAry avatar Feb 07 '25 16:02 MatthewAry

aliasedTable is only for use in the RQB API. I see that the docs uses that function instead of alias: https://orm.drizzle.team/docs/joins#aliases--selfjoins.

We'll fix this documentation issue.

Still not fixed, thanksfully I found this GH issue since I was VERY confused.

aleksasiriski avatar Feb 08 '25 11:02 aleksasiriski

Image How to replace it?

ledihildawan avatar Mar 14 '25 18:03 ledihildawan