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

[BUG]: Left join not working after updating to latest

Open thillmann opened this issue 1 year ago • 2 comments

What version of drizzle-orm are you using?

0.25.4

What version of drizzle-kit are you using?

0.17.6

Describe the Bug

After updating to the latest version of drizzle-orm, my left joins are not working anymore.

Every time I run a query that includes a join it just errors with:

Error: Your "users->id" field references a column "users"."id", but the table "users" is not part of the query! Did you forget to join it?

I have two tables for users and accounts like this:

const usersTable = mysqlTable(
  "users",
  {
    id: varchar("id", { length: 191 }).primaryKey().notNull(),
    createdAt: datetime("created_at", { fsp: 3 }).notNull(),
    name: varchar("name", { length: 191 }),
    email: varchar("email", { length: 191 }).notNull(),
    emailVerified: datetime("email_verified", { fsp: 3 }),
    image: text("image"),
  },
  (table) => ({
    emailIdx: uniqueIndex("email_idx").on(table.email),
  })
);

const accountsTable = mysqlTable(
  "accounts",
  {
    id: varchar("id", { length: 191 }).primaryKey().notNull(),
    userId: varchar("user_id", { length: 191 }).notNull(),
    type: varchar("type", { length: 191 }).notNull(),
    provider: varchar("provider", { length: 191 }).notNull(),
    providerAccountId: varchar("provider_account_id", {
      length: 191,
    }).notNull(),
    refreshToken: text("refresh_token"),
    accessToken: text("access_token"),
    expiresAt: int("expires_at"),
    tokenType: varchar("token_type", { length: 191 }),
    scope: varchar("scope", { length: 191 }),
    idToken: text("id_token"),
    sessionState: varchar("session_state", { length: 191 }),
  },
  (table) => ({
    providerProviderAccountIdIdx: uniqueIndex(
      "provider_provider_account_id_idx"
    ).on(table.provider, table.providerAccountId),
  })
);

And then a query trying to join them like this:

const [userAndAccount] = await db
      .select({ user: usersTable, account: accountsTable })
      .from(accountsTable)
      .leftJoin(usersTable, eq(accountsTable.userId, usersTable.id))
      .where(
        and(
          eq(accountsTable.provider, provider),
          eq(accountsTable.providerAccountId, providerAccountId)
        )
      )
      .limit(1);

This works fine on 0.23.13.

Expected behavior

It shouldn't error since I'm clearly joining the tables.

Environment & setup

I'm using Planetscale and set up the connection as follows:

import { drizzle } from "drizzle-orm/planetscale-serverless";

const connection = connect({
  host: process.env.DATABASE_HOST,
  username: process.env.DATABASE_USERNAME,
  password: process.env.DATABASE_PASSWORD,
});

const db = drizzle(connection);

thillmann avatar May 03 '23 01:05 thillmann

Thanks! Will check it asap

AndriiSherman avatar May 03 '23 06:05 AndriiSherman

@thillmann could you test if it works with drizzle-orm@beta?

dankochetov avatar May 03 '23 10:05 dankochetov

@dankochetov

Sorry for the delayed response, I can confirm it works with drizzle-orm@beta!

thillmann avatar May 10 '23 08:05 thillmann