On Oct 12, 2012, at 5:10 AM, Stefano Fontanelli wrote:

> 
> Hi,
> I'm working on the new release of ColanderAlchemy ( 
> https://github.com/stefanofontanelli/ColanderAlchemy ) which will use the new 
> SQLA 0.8 inspection API.
> 
> 
> The new version of CA will use 'info' kwarg which can be specified in both 
> Column and relationship.
> I found also 'info' kwargs in 'sqlalchemy.orm.properties.ColumnProperty' but 
> it is empty when I specify info as in the example below:
> 
> class Person(Base):
> 
>    __tablename__ = 'people'
> 
>    id = Column(Integer, primary_key=True)
>    name = Column(Unicode(128), nullable=False)
>    surname = Column(Unicode(128), nullable=False, info={'test': 'column'})
>    addresses = relationship('Address', info={'test': ' relationship'})
> 
> 
> m = inspect(Person)
> 
> print m.attrs.surname.info  --> {}
> print m.attrs.surname.columns[0].info --> {'test': 'column'}
> print m.attrs.addresses.info --> {'test': 'relationship'}
> 
> 
> My questions are simple and n00b :)
> 1) Did you plan to populate ColumnProperty.info with the content of 
> Column.info?

Definitely not yet.   The ColumnProperty and the Column are different things 
and are analogous to the relationship() and the remote class.   In the latter 
case it's easy to see how those two things are different.  In the former, it's 
being a bit more pedantic, but ColumnProperty can have multiple Columns in it, 
and a Column can be owned by more than one ColumnProperty.   So just default 
updating ColumnProperty.info with the .info of all the Columns I'm not ready to 
go there.  It's really a many-to-many between these two types of objects.

Another thing I thought of is, OK what if ColumnProperty had some other kind of 
.info, like ".all_info" or something, and everyone calls that.  But that seems 
a little crazy too.

The good news is we can defer making any decisions in SQLA and you can get what 
you want using an event.  There's actually two approaches I can see for this, 
I've also come across some event listening inconsistencies I want to fix up, 
but here they are:

from sqlalchemy.orm import ColumnProperty

@event.listens_for(Base, "attribute_instrument")
def copy_info(cls, key, inst):
    prop = inst.property
    if isinstance(prop, ColumnProperty):
        for col in prop.columns:
            prop.info.update(col.info)


or

from sqlalchemy.orm import mapper

@event.listens_for(mapper, "mapper_configured")
def configure(mapper, cls):
    for prop in mapper.column_attrs:
        for col in prop.columns:
            prop.info.update(col.info)


note that the first listens_for() up there against Base is actually global to 
all classes at the moment.  That's a bug.  Both of these events are currently 
global to all mappings right now.   

> 2) Is access of Column.info the right way to 'read' Column.info specified by 
> the user in declarative example?

if you want to get at the .info on Column then yes, but for your purposes it's 
probably nice to clean it up as above.


-- 
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en.

Reply via email to