© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
1 reply
Liam

Drizzle query with where clause and primary composite key

Hello, I have defined a composite key and am now trying to run a drizzle query to lookup the event where that key exists. I have defined the following query, is this the fastest way to go about doing this lookup? Thanks!

Drizzle Schema Table:
export const scans = pgTable(
    "scans",
    {
        createdAt: timestamp("created_at").notNull().defaultNow(),
        userID: varchar("user_id", { length: 255 }).notNull(),
        eventID: integer("event_id").notNull(),
        count: integer("count").notNull(),
    },
    (table) => ({
        id: primaryKey({ columns: [table.userID, table.eventID] }),
    })
);
export const scans = pgTable(
    "scans",
    {
        createdAt: timestamp("created_at").notNull().defaultNow(),
        userID: varchar("user_id", { length: 255 }).notNull(),
        eventID: integer("event_id").notNull(),
        count: integer("count").notNull(),
    },
    (table) => ({
        id: primaryKey({ columns: [table.userID, table.eventID] }),
    })
);


Lookup:

const scan = await db.query.scans.findFirst({
  where: and(eq(scans.eventID, eventID), eq(scans.userID, userID)),
});
const scan = await db.query.scans.findFirst({
  where: and(eq(scans.eventID, eventID), eq(scans.userID, userID)),
});


Thanks for the help!
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

Composite Primary Key?
Drizzle TeamDTDrizzle Team / help
16mo ago
reference composite primary key
Drizzle TeamDTDrizzle Team / help
3y ago
Composite primary key, multiple primary keys
Drizzle TeamDTDrizzle Team / help
2y ago
Compostie primary key of a composite primary key
Drizzle TeamDTDrizzle Team / help
2y ago