Rules Hub
Coding Rules Library
← Back to all rules
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
backend ruleP1universalStack: sql
sqlidempotencyunique-constrainterror-handling
Make insert-if-not-exists operations idempotent with ON CONFLICT DO NOTHING
Inserts protected by a unique constraint should use conflict-do-nothing so retries/double-clicks don't surface errors when the desired state already holds.
PR: hegnar-journalist-boost · org-mining-deep-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | await db.insert(favorites).values({ userId, category }); |
| 2 | // second click throws on the unique (userId, category) constraint |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | await db.insert(favorites) |
| 2 | .values({ userId, category }) |
| 3 | .onConflictDoNothing(); |
Explanation (EN)
Objašnjenje (HR)