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]: Query builder does not work with mutiple schemas #3566

Open
1 task done
Highlighted-dev opened this issue Nov 15, 2024 · 0 comments
Open
1 task done

[BUG]: Query builder does not work with mutiple schemas #3566

Highlighted-dev opened this issue Nov 15, 2024 · 0 comments
Labels
bug Something isn't working

Comments

@Highlighted-dev
Copy link

Highlighted-dev commented Nov 15, 2024

Report hasn't been filed before.

  • I have verified that the bug I'm about to report hasn't been filed before.

What version of drizzle-orm are you using?

0.36.3

What version of drizzle-kit are you using?

0.36.3

Other packages

No response

Describe the Bug

For my drizzle setup, I am using 3 files (example simplified) and postgresql db:

index.ts

import dotenv from "dotenv";
import { drizzle } from "drizzle-orm/postgres-js";
import postgres from "postgres";

import * as schema2 from "./schema2";
import * as schema from "./schema";
dotenv.config();

const globalForDb = globalThis as unknown as {
  conn: postgres.Sql | undefined;
};
const conn = globalForDb.conn ?? postgres(process.env.DATABASE_URL!);
if (process.env.NODE_ENV !== "production") globalForDb.conn = conn;

export const db = drizzle(conn, { schema: schema });
export const db2 = drizzle(conn, { schema: schema2 });

schema.ts

export const createTable = pgTableCreator((name) => `schema1_${name}`);

export const users = createTable("user", {
  id: varchar("id", { length: 255 })
    .notNull()
    .primaryKey()
    .$defaultFn(() => crypto.randomUUID()),
  name: varchar("name", { length: 255 }),
  email: varchar("email", { length: 255 }).notNull(),
  emailVerified: timestamp("email_verified", {
    mode: "date",
    withTimezone: true,
  }).default(sql`CURRENT_TIMESTAMP`),
  image: varchar("image", { length: 255 }),
  role: varchar("role", { length: 255 }).notNull().default("user"),
});

schema2.ts

export const createTable = pgTableCreator((name) => `schema2_${name}`);

export const comments = createTable(
  "comments",
  {
    id: serial("id").primaryKey(),
    content: text("content").notNull(),
    createdAt: timestamp("created_at", { withTimezone: true })
      .default(sql`CURRENT_TIMESTAMP`)
      .notNull(),
    updatedAt: timestamp("updated_at", { withTimezone: true }).$onUpdate(
      () => new Date()
    ),
    votes: integer("votes").default(0).notNull(),
  }
);

export const commentsRelations = relations(comments, ({ one }) => ({
  user: one(users, { fields: [comments.userId], references: [users.id] }),
}));

The comments in the schema2 are imported from schema1.
The problem is that I can see the relation in drizzle studio and I can get the table with select(), but I can't get the comments with user in the query builder.

Example select() (this works):

    const comments1 = await db
      .select()
      .from(comments)
      .innerJoin(users, eq(comments.userId, users.id));

Example query builder (this doesnt work):

    const comments2 = await db.query.comments.findMany({
      with: {
        user: true,
      },
    });

If I try to use query builder, I am getting following error
Error: Cannot read properties of undefined (reading 'columns')

@Highlighted-dev Highlighted-dev added the bug Something isn't working label Nov 15, 2024
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

1 participant