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]: Left join not working after updating to latest #542

Closed
thillmann opened this issue May 3, 2023 · 3 comments
Closed

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

thillmann opened this issue May 3, 2023 · 3 comments
Assignees
Labels
bug Something isn't working

Comments

@thillmann
Copy link

thillmann commented May 3, 2023

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 thillmann added the bug Something isn't working label May 3, 2023
@AndriiSherman
Copy link
Member

Thanks! Will check it asap

@AndriiSherman AndriiSherman self-assigned this May 3, 2023
@dankochetov
Copy link
Contributor

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

@thillmann
Copy link
Author

thillmann commented May 10, 2023

@dankochetov

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

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

3 participants