3
0
Fork 0

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
This commit is contained in:
Lance Edgar 2025-01-12 19:42:22 -06:00
parent c33f211633
commit 8ba44e10bd

View file

@ -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}")