PY-SQLA0005 · sqlmodel_primary_key_get
Prefer Session.get for provable SQLModel primary-key lookups.
This is a deterministic rule for python. Read its implementation.
Definition
Section titled “Definition”Report the exact chain session.exec(select(Item).where(Item.id == key)).first() when the same
module declares Item as a SQLModel table and marks id as a primary key. The rule requires a
resolved SQLModel session, one selected model, one equality predicate, and no execution
options.
Evidence
Section titled “Evidence”Each finding points to one complete primary-key lookup chain. The value is the number of exact chains.
Exceptions
Section titled “Exceptions”Composite keys, imported models, aliases, statement variables, additional predicates, eager
loading options, and non-first result contracts remain unreported. They can change query
semantics and need explicit review.
Examples
Section titled “Examples”hero = session.exec(select(Hero).where(Hero.id == hero_id)).first()hero = session.get(Hero, hero_id)References
Section titled “References”- Cites “SQLModel documentation”, primary-key lookup guidance. Open reference
- Cites “SQLAlchemy documentation”, Session.get identity-map contract. Open reference