I just want to programatically determine the name of an SQLalchemy model's primary key.
I just want to programatically determine the name of an SQLalchemy model's primary key.
Assuming the declarative model class is User,
>>> list(User.__table__.primary_key)[0].name
'id'
Or for Membership which has a composite primary key
>>> [pk.name for pk in Membership.__table__.primary_key]
['user_id', 'group_id']