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
    }));


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
    };


Do I need to use a primary (non-composite) key in the friends table?
Was this page helpful?