© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
9 replies
xamarot

How to use "onConflictDoUpdate" with composite key?

If a user sends a friend request while one exists it should update to pending. But the "friends" table is using a composite key.

Table looks like this

export const friends = pgTable("friends", {
    senderUsername: varchar("sender_username").notNull(),
    recipientUsername: varchar("recipient_username").notNull(),
    sentAt: date("sent_at").defaultNow().notNull(),
    repliedAt: date("replied_at"),

    requestStatus: requestStatusEnum("request_status").notNull().default("PENDING")
},
    (t) => ({
        pk: primaryKey(t.senderUsername, t.recipientUsername) <- composite key
    }));
export const friends = pgTable("friends", {
    senderUsername: varchar("sender_username").notNull(),
    recipientUsername: varchar("recipient_username").notNull(),
    sentAt: date("sent_at").defaultNow().notNull(),
    repliedAt: date("replied_at"),

    requestStatus: requestStatusEnum("request_status").notNull().default("PENDING")
},
    (t) => ({
        pk: primaryKey(t.senderUsername, t.recipientUsername) <- composite key
    }));


query looks like this

    await db.insert(friends).values({
        senderUsername,
        recipientUsername
    }).onConflictDoUpdate({ target: friends.senderUsername, // <- how to do this?
        set: { requestStatus: REQUEST_STATUS.PENDING } });

    return {
        sendFriendRequestForm
    };
    await db.insert(friends).values({
        senderUsername,
        recipientUsername
    }).onConflictDoUpdate({ target: friends.senderUsername, // <- how to do this?
        set: { requestStatus: REQUEST_STATUS.PENDING } });

    return {
        sendFriendRequestForm
    };


Do I need to use a primary (non-composite) key in the friends table?
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

Does onConflictDoUpdate work with composite primary keys?
Drizzle TeamDTDrizzle Team / help
3y ago
How to use a composite primary key in WHERE?
Drizzle TeamDTDrizzle Team / help
3y ago
Strugling to use composite key in schema
Drizzle TeamDTDrizzle Team / help
3y ago
How to reference composite key in foreign key
Drizzle TeamDTDrizzle Team / help
2y ago