Skip to content

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.

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.

Each finding points to one complete primary-key lookup chain. The value is the number of exact chains.

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.

hero = session.exec(select(Hero).where(Hero.id == hero_id)).first()
hero = session.get(Hero, hero_id)
  • Cites “SQLModel documentation”, primary-key lookup guidance. Open reference
  • Cites “SQLAlchemy documentation”, Session.get identity-map contract. Open reference