ZX
ZX
Explore posts from servers
DTDrizzle Team
Created by ZX on 2/25/2024 in #help
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)
1 replies