Last active
May 7, 2019 09:08
-
-
Save kataev/76d1bc42441d4cea65c2c952d490a603 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sqlalchemy as sa | |
from sqlalchemy.ext.declarative import declarative_base | |
Base = declarative_base() | |
class Test(Base): | |
__tablename__ = 'test' | |
id = sa.Column('test_id', sa.Integer, primary_key=True) | |
# we have only column and instance | |
some_col = Test.__table__.c.test_id | |
t = Test() | |
t.id = 1 | |
# now try find value in instance by column | |
col_name = some_col.name | |
try: | |
getattr(t, col_name) # bug in sqlakeyset | |
except AttributeError: | |
print('attr err') # error sad but true | |
from sqlalchemy.orm import object_mapper | |
mapper = object_mapper(t) | |
col_property = mapper.get_property_by_column(some_col) | |
val = getattr(t, col_property.key) | |
assert val == t.id |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment