wvn
Explore posts from serversPPrisma
•Created by wvn on 1/10/2025 in #help-and-questions
Error saving in database
I have this code but it save to the db
Unique constraint failed on the constraint: `UkladanieRole_username_key`
at Vn.handleRequestError (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:121:7339)
at Vn.handleAndLogRequestError (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:121:6663)
at Vn.request (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:121:6370)
at async l (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:130:9617)
at async execute (file:///C:/Users/wvn_0/Desktop/GalaxyBOT/bot/dist/events/discord/leave.js:41:9) {
code: 'P2002',
clientVersion: '6.2.1',
meta: { modelName: 'UkladanieRole', target: 'UkladanieRole_username_key' }
}
Unique constraint failed. It seems that the username already exists in the database.
Unique constraint failed on the constraint: `UkladanieRole_username_key`
at Vn.handleRequestError (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:121:7339)
at Vn.handleAndLogRequestError (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:121:6663)
at Vn.request (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:121:6370)
at async l (C:\Users\wvn_0\Desktop\GalaxyBOT\bot\node_modules\@prisma\client\runtime\library.js:130:9617)
at async execute (file:///C:/Users/wvn_0/Desktop/GalaxyBOT/bot/dist/events/discord/leave.js:41:9) {
code: 'P2002',
clientVersion: '6.2.1',
meta: { modelName: 'UkladanieRole', target: 'UkladanieRole_username_key' }
}
Unique constraint failed. It seems that the username already exists in the database.
const existingUser = await prisma.ukladanieRole.findUnique({
where: { username: c.user.username },
});
try {
if (existingUser) {
console.log("Existing User! Updating the record...");
// Update the existing user's roles
await prisma.ukladanieRole.update({
where: { username: c.user.username },
data: { rolesID: roleIds },
});
console.log(`Updated roles for user: ${c.user.username}`);
} else {
console.log("New User! Creating a new record...");
// Create a new record for the user
await prisma.ukladanieRole.create({
data: {
username: c.user.username,
userID: c.id,
rolesID: roleIds,
},
});
console.log(`Created new record for user: ${c.user.username}`);
}
const existingUser = await prisma.ukladanieRole.findUnique({
where: { username: c.user.username },
});
try {
if (existingUser) {
console.log("Existing User! Updating the record...");
// Update the existing user's roles
await prisma.ukladanieRole.update({
where: { username: c.user.username },
data: { rolesID: roleIds },
});
console.log(`Updated roles for user: ${c.user.username}`);
} else {
console.log("New User! Creating a new record...");
// Create a new record for the user
await prisma.ukladanieRole.create({
data: {
username: c.user.username,
userID: c.id,
rolesID: roleIds,
},
});
console.log(`Created new record for user: ${c.user.username}`);
}
18 replies