Hi all,from a given class I'd like to determine which attributes are columns, not only physical columns present in the underlying table, but also those from a relationship(). The FAQ seems to provide an answer ("What's the best way to figure out which attributes are columns, given a class"), but the proposed solution throws an "sqlalchemy.orm.exc.UnmappedClassError: Class '<pym.models.usrmgr.Principal object at 0x3a69c10>' is not mapped" error in my setup.
I'm using 0.7.2. with declarative classes, e.g.DbBase = declarative_base()class Principal(DbBase, PymMixin):__tablename__ = "principal"__table_args__ = {'schema': 'pym'}is_enabled = Column(Boolean, nullable=False, default=False)...Does is matter that I try to read the attributes from a query?qry = sess.query(Principal)for p in qry:print p.display_name# Determine columns in last looped entitymapper = class_mapper(p) #<---- this throws UnmappedClassError
OK, little terminology gap here, there's only one kind of "column" in SQLAlchemy and that's "Column", a relationship() is not in any way describable as a "column", so we just call them at the class level "attributes" and at the mapper level "properties". You're looking at properties, each of which is MapperProperty. They all have a key, so easy enough:
print "Attribute keys:", ",".join(prop.key for prop in object_mapper(p).iterate_properties)
ColumnProperty and RelationshipProperty are the two prominent types, others include CompositeProperty and SynonymProperty.