Unique constrain error, id still incremented

Hi folks, so I have moved from TypeORM to DrizzleORM and I love it! I encountered an issue recently where I have a unique constrain on my email field:
export const users = pgTable("users", {
...
email: varchar("email", { length: 256 }).unique().notNull(),
});
export const users = pgTable("users", {
...
email: varchar("email", { length: 256 }).unique().notNull(),
});
When I try to save the user, everything works, I get my user with id , for example 10. Now when I try to save the same user, I get my nice error message: "detail": "Key (email)=(john.doe@example.com) already exists.". The issue comes when I try to save a new user now. Instead of saving a new user with id 11, it skips the one it tried to save when unique constrain error was thrown and save the new user with id 12. Any ideas how to fix this please ?
3 Replies
Angelelz
Angelelz9mo ago
This is how the database will work There is a way to reset the count in any case
Vladimír Gál
Vladimír Gál9mo ago
Cheers mate, if that's how it should behave then I can live with it. Was just wondering if I am not doing anything wrong. Thanks!
Angelelz
Angelelz9mo ago
Stack Overflow
Reset auto increment counter in postgres
I would like to force the auto increment field of a table to some value, I tried with this: ALTER TABLE product AUTO_INCREMENT = 1453 AND ALTER SEQUENCE product RESTART WITH 1453; ERROR: relation &
Want results from more Discord servers?
Add your server