DT
Drizzle Team10mo ago
ZX

Getting duplicate key value error with many to many relations

let rows = await database
.select()
.from(usersLikeImages)
.where(
and(
eq(usersLikeImages.userId, session.user.id),
eq(usersLikeImages.imageId, imageId),
),
);

if (rows.length > 0) {
await database
.delete(usersLikeImages)
.where(
and(
eq(usersLikeImages.userId, session.user.id),
eq(usersLikeImages.imageId, imageId),
),
);
} else {
await database.insert(usersLikeImages).values({
userId: session.user.id,
imageId,
});
}
let rows = await database
.select()
.from(usersLikeImages)
.where(
and(
eq(usersLikeImages.userId, session.user.id),
eq(usersLikeImages.imageId, imageId),
),
);

if (rows.length > 0) {
await database
.delete(usersLikeImages)
.where(
and(
eq(usersLikeImages.userId, session.user.id),
eq(usersLikeImages.imageId, imageId),
),
);
} else {
await database.insert(usersLikeImages).values({
userId: session.user.id,
imageId,
});
}
export const usersLikeImages = pgTable("users_like_images", {
userId: text("user_id")
.notNull()
.references(() => users.id),
imageId: uuid("image_id")
.notNull()
.references(() => images.id),
});
export const usersLikeImages = pgTable("users_like_images", {
userId: text("user_id")
.notNull()
.references(() => users.id),
imageId: uuid("image_id")
.notNull()
.references(() => images.id),
});
Does anyone know what might be causing this error?
web:dev: ⨯ ../node_modules/postgres/cjs/src/connection.js (790:25) @ postgres
web:dev: ⨯ PostgresError: duplicate key value violates unique constraint "users_like_images_pkey"
web:dev: at Socket.emit (node:events:518:28)
web:dev: ⨯ ../node_modules/postgres/cjs/src/connection.js (790:25) @ postgres
web:dev: ⨯ PostgresError: duplicate key value violates unique constraint "users_like_images_pkey"
web:dev: at Socket.emit (node:events:518:28)
0 Replies
No replies yetBe the first to reply to this messageJoin
Want results from more Discord servers?
Add your server