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 ruleP0universalStack: sql
data-modellingdatabaseconstraintsdata-integrity
Enforce intended uniqueness with a database constraint, not just application logic
When the design requires at most one row per key combination, declare a composite UNIQUE index at the database/model level so duplicates are impossible regardless of application code paths.
PR: vinify-backend · org-mining-2026-06Created: Jun 17, 2026
Bad example
Old codetypescript
| 1 | @Table({ tableName: 'selling_price_toggles' }) |
| 2 | export class SellingPriceToggle { |
| 3 | @Column cellarId!: number; |
| 4 | @Column slot!: number; // nothing prevents duplicate (cellarId, slot) |
| 5 | } |
Explanation (EN)
Objašnjenje (HR)
Good example
New codetypescript
| 1 | @Table({ |
| 2 | tableName: 'selling_price_toggles', |
| 3 | indexes: [{ unique: true, fields: ['cellar_id', 'slot'] }], |
| 4 | }) |
| 5 | export class SellingPriceToggle { |
| 6 | @Column cellarId!: number; |
| 7 | @Column slot!: number; |
| 8 | } |
Explanation (EN)
Objašnjenje (HR)