Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
iSchoen opened this issue Sep 18, 2024 · 3 comments
Labels
bug Something isn't working

Comments

@iSchoen
Copy link

iSchoen commented Sep 18, 2024

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 iSchoen added the bug Something isn't working label Sep 18, 2024
@lasseklovstad
Copy link

I just encountered the same issue today

@leonardorb
Copy link

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

@gklee555
Copy link

I'm having the same issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants