Skip to content

fix: unwrap Enum values in get_object_identifier#1092

Open
pctablet505 wants to merge 2 commits into
smithyhq:mainfrom
pctablet505:fix/enum-primary-key-identifier
Open

fix: unwrap Enum values in get_object_identifier#1092
pctablet505 wants to merge 2 commits into
smithyhq:mainfrom
pctablet505:fix/enum-primary-key-identifier

Conversation

@pctablet505

Copy link
Copy Markdown

Summary

get_object_identifier() returns the raw primary key value(s) for use in URLs and ajax lookups. For models whose primary key is an Enum column, it was returning the Enum member itself rather than its value, so str() on it produced AnimalEnum.DOG instead of dog. This broke generated edit/detail URLs for such models (e.g. /admin/animal/edit/AnimalEnum.DOG).

This unwraps Enum members to their .value before use, for both single and composite primary keys.

Closes #955

Test plan

  • Added test_single_pk_identifier_with_enum in tests/test_helpers.py, covering a model with an Enum primary key.
  • uv run pytest --cov=sqladmin --cov-report=term-missing — 484 passed, 4 skipped (pre-existing), coverage 97.10%.
  • make lint (ruff check, ruff format --check, mypy) — all pass.

get_object_identifier() returned the raw Enum member for models with
an Enum primary key, so str() on it produced "ClassName.MEMBER"
instead of the enum's value. This broke generated URLs and ajax
identifiers for such models.

Closes smithyhq#955
@mmzeynalli

Copy link
Copy Markdown
Member

Interesting. Is it somehow common to have enum field as primary key? PR seems okay, but I find it obsolete, as this is not standard and not good practice to use enum as pk

The prior assertion (isinstance(identifier, str)) passed even without the
unwrap fix, since AnimalEnum subclasses str directly. Assert type(identifier)
is str instead, so the test actually fails if get_object_identifier stops
unwrapping the enum.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug: get_object_identifier returns Enum instance instead of string value for Enum primary keys

2 participants