Rules Hub
Coding Rules Library
Rule priority, scope & exceptions
Use this to align rules with the senior-level structure (P0/P1/P2, scope, exceptions/tradeoffs).
Declare a primary key on every table schema
ORM/SQL table definitions should declare a primary key, consistent with sibling tables; a missing PK is an easy-to-miss correctness gap.
Bad example
| 1 | export const corporateActions = pgTable('corporateactions', { |
| 2 | insref: bigint('insref', { mode: 'number' }).notNull(), |
| 3 | date: date('date').notNull(), |
| 4 | type: smallint('type').notNull(), |
| 5 | // ...many columns, but no primary key declared |
| 6 | }); |
Explanation (EN)
Without a primary key the schema can't express row identity. Upserts, dedupe, and tooling that relies on a PK will misbehave, and it's inconsistent with sibling tables that do declare one.
Objašnjenje (HR)
Bez primarnog kljuca shema ne moze izraziti identitet retka. Upsert, deduplikacija i alati koji se oslanjaju na PK ce krivo raditi, a i nekonzistentno je sa srodnim tablicama koje ga deklariraju.
Good example
| 1 | import { primaryKey } from 'drizzle-orm/pg-core'; |
| 2 |
|
| 3 | export const corporateActions = pgTable( |
| 4 | 'corporateactions', |
| 5 | { |
| 6 | insref: bigint('insref', { mode: 'number' }).notNull(), |
| 7 | date: date('date').notNull(), |
| 8 | type: smallint('type').notNull(), |
| 9 | // ... |
| 10 | }, |
| 11 | (t) => ({ |
| 12 | pk: primaryKey({ columns: [t.insref, t.date, t.type] }), |
| 13 | }), |
| 14 | ); |
Explanation (EN)
Declaring a (composite) primary key matches the pattern of sibling tables and makes row identity explicit for upserts, replication, and ORM tooling.
Objašnjenje (HR)
Deklariranje (slozenog) primarnog kljuca prati uzorak srodnih tablica i cini identitet retka eksplicitnim za upsert, replikaciju i ORM alate.
Notes (EN)
When adding a new table, copy the conventions of an existing comparable table in the same schema folder rather than defining columns in isolation.
Bilješke (HR)
Kad dodajes novu tablicu, preuzmi konvencije postojece usporedive tablice iz iste mape sheme umjesto da definiras stupce izolirano.