Skip to content
Sarj Code Standards

store-field-update-reuses-upsert

A plain field UPDATE duplicates fields already writable through the store's upsert command.

Why

Independent per-field SQL methods duplicate write policy and can drift from the existing model command's validation, conflict handling and timestamps.

Fix

Prefer one typed model command for ordinary writable fields. Reuse or extend its write implementation while preserving update-only, missing-row, omission/null, authorization and concurrency contracts; retain domain transitions as separate operations.

Examples

Before — flagged Consolidate an ordinary field write
app/user_store.py
from pydantic import BaseModel
class UserInput(BaseModel):
id: str
name: str
class UserStore:
async def upsert(self, data: UserInput) -> User:
await self.cursor.execute(
"INSERT INTO users (id, name) VALUES (%s, %s) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name",
(data.id, data.name),
)
async def set_name(self, id_: str, name: str) -> User:
await self.cursor.execute(
"UPDATE users SET name = %s WHERE id = %s", (name, id_)
)
After — preferred Keep an update with a distinct concurrency contract
app/user_store.py
from pydantic import BaseModel
class UserInput(BaseModel):
id: str
name: str
class UserStore:
async def upsert(self, data: UserInput) -> User:
await self.cursor.execute(
"INSERT INTO users (id, name) VALUES (%s, %s) ON CONFLICT (id) DO UPDATE SET name = EXCLUDED.name",
(data.id, data.name),
)
async def set_name(self, id_: str, name: str, version: int) -> User:
await self.cursor.execute(
"UPDATE users SET name = %s WHERE id = %s AND version = %s",
(name, id_, version),
)