From 8ba44e10bd4a0e1fc5d94848e35bfa1ba9d4404a Mon Sep 17 00:00:00 2001 From: Lance Edgar Date: Sun, 12 Jan 2025 19:42:22 -0600 Subject: [PATCH] fix: use prop key instead of column name, for master view model key every once in a while those can differ, we need prop key when they do --- src/wuttaweb/views/master.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/wuttaweb/views/master.py b/src/wuttaweb/views/master.py index cdea8c7..cb32ef9 100644 --- a/src/wuttaweb/views/master.py +++ b/src/wuttaweb/views/master.py @@ -2707,6 +2707,9 @@ class MasterView(View): represents a Wutta-based SQLAlchemy model, the return value for this method is: ``('uuid',)`` + Any class mapped via SQLAlchemy should be supported + automatically, the keys are determined from class inspection. + But there is no "sane" default for other scenarios, in which case subclass should define :attr:`model_key`. If the model key cannot be determined, raises ``AttributeError``. @@ -2721,8 +2724,12 @@ class MasterView(View): model_class = cls.get_model_class() if model_class: - mapper = sa.inspect(model_class) - return tuple([column.key for column in mapper.primary_key]) + # nb. we want the primary key but must avoid column names + # in case mapped class uses different prop keys + inspector = sa.inspect(model_class) + keys = [col.name for col in inspector.primary_key] + return tuple([prop.key for prop in inspector.column_attrs + if [col.name for col in prop.columns] == keys]) raise AttributeError(f"you must define model_key for view class: {cls}")