How to identify that an InstrumentedAttribute or its ColumnProperty are read-only?

267 views
Skip to first unread message

Peter Bunyan

unread,
Dec 12, 2012, 10:07:21 AM12/12/12
to sqlal...@googlegroups.com
Much like FormAlchemy, I am trying to update my model from a post in a generic manner. I'd like to find out if an Attribute(InstrumentedAttribute) or its associated ColumnProperty is read-only. (please excuse ridiculous example)

class Foo(Base):    
    id = Column(Integer, primary_key=True)
    name = Column(String)    
    title = column_property(
        select([func.UPPER(name)]).\
        where(id==id)
    )

Although setting 'title' will not cause an insert or update in the db transaction, as it is a column_property, it will be settable. I'd like to not set the attribute by using class_mapper(Foo).iterate_properties and being able to determine that it was read_only. I do not ask lightly. I've read extensively and looked through the source code and come up blank. I also acknowledge that I wrote the model so I know it's read only - but I'd rather be lazy and get my update to do the work - after all, that is why I'm using such an excellent ORM.


 

Michael Bayer

unread,
Dec 12, 2012, 11:15:53 AM12/12/12
to sqlal...@googlegroups.com
the column_property has a .columns collection, of which it is usually just columns[0].   If columns[0] is present in the mapped table, the ORM will write to it; otherwise not.

So :

mapper = class_mapper(Foo)

table = mapper.local_table

property = Foo.name.property

expression = property.columns[0]

is_a_regular_mapped_column = table.c.contains_column(expression)







 

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To view this discussion on the web visit https://groups.google.com/d/msg/sqlalchemy/-/kFNDPpm3J9EJ.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Peter Bunyan

unread,
Dec 12, 2012, 11:58:52 AM12/12/12
to sqlal...@googlegroups.com
Thank you so much. Wonderful!
Reply all
Reply to author
Forward
0 new messages