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).
Scope an UPDATE to the exact row you matched
Don't filter an UPDATE by a foreign key alone when that key isn't unique on the target table.
Bad example
| 1 | await db.update(profiles) |
| 2 | .set({ contactEmail: email }) |
| 3 | .where(and(eq(profiles.personId, personId), isNull(profiles.contactEmail))); |
Explanation (EN)
personId isn't unique on profiles — a person can own more than one profile row after a merge — so this writes the email to every matching row instead of just the one that was looked up, and a re-run can fan the value across rows created by a later merge.
Objašnjenje (HR)
personId nije jedinstven na profiles — osoba može imati više redaka profila nakon spajanja — pa ovo upisuje e-mail u svaki podudarni redak umjesto samo onaj koji je dohvaćen, a ponovno pokretanje može raširiti vrijednost na retke nastale kasnijim spajanjem.
Good example
| 1 | await db.update(profiles) |
| 2 | .set({ contactEmail: email }) |
| 3 | .where(and(eq(profiles.id, matchedProfileId), isNull(profiles.contactEmail))); |
Explanation (EN)
Scope the WHERE clause to the primary key of the exact row that was matched earlier, not a foreign key that may point at several rows.
Objašnjenje (HR)
Ograniči WHERE uvjet na primarni ključ točnog retka koji je ranije pronađen, a ne na strani ključ koji može pokazivati na više redaka.