Skip to content

bug: get-by-id field raises KeyError when id_field_name differs from the primary key attribute #413

Closed

Description

@gazorby

Description

A single-type root field built by strawchemy.field() exposes its primary key argument under id_field_name (config.default_id_field_name, or the field's own id_field_name), but the repository looks the value up under the primary key attribute name.

  • src/strawchemy/schema/field.py (auto_arguments): for a single primary key, the argument is StrawberryArgument(self.id_field_name, ...).
  • src/strawchemy/repository/sqlalchemy/_async.py (get_one/by-id lookup) and its _sync.py counterpart: kwargs.pop(field_def.name), where field_def.name is the mapper's primary key attribute.

When the two names differ, the lookup raises KeyError.

Reproduction

strawchemy = Strawchemy(StrawchemyConfig("postgresql", default_id_field_name="pk"))

@strawchemy.type(Fruit, include="all")
class FruitType: ...

@strawberry.type
class Query:
    fruit: FruitType = strawchemy.field()

{ fruit(pk: 1) { name } } fails with KeyError: 'id'.

tests/unit/schemas/custom_id_field_name.py only snapshots the schema, so no test executes this path. Composite primary keys are unaffected: their arguments use the attribute names.

Activity

  1. changed the title [-]bug: get-by-id field raises KeyError when id_field_name differs from the primary key attribute[/-] [+]bug: `get-by-id` field raises KeyError when `id_field_name` differs from the primary key attribute[/+] on Oct 8, 2026
  2. changed the title [-]bug: `get-by-id` field raises KeyError when `id_field_name` differs from the primary key attribute[/-] [+]bug: `get-by-id` field raises `KeyError` when `id_field_name` differs from the primary key attribute[/+] on Oct 8, 2026
  3. added 4 commits that reference this issue on Oct 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions