How do I PROGRAMATICALLY find the columns defined in a declarative SQLAlchemy table? I already know how to do it with generic Python reflection, but I was hoping for something more direct. To date, no luck finding it.
Example:
class SomeTable(Base): # The standard declarative base
__tablename__ = "hello"
col1 = Column(Integer)
col2 = Column(String(10))
I would like to say:
new_row = SomeTable(...)
...
a lot of code, maybe passing "new_row" to a function
...
either_column_name_list_or_dict = new_row.columns()
and less attractively (but still useful, as classes can be record-kept, too):
either_column_name_list_or_dict = SomeTable.columns()
Frankly, the dict of column names/values or column name/Column objects is better, but I would accept just a list of column names, too (as I can call getattr
on them)
To proactively answer the 5000 "Why are you doing that?" replies, I have a very specific need for this (record keeping-based). But that is irrelevant here. Thanks!