On Jan 4, 2011, at 2:34 PM, Martijn Moeling wrote:

> Michael,
> 
> Thank you, 
> 
> reading trough your example (and running it)
> made me understand a bit more how SQLA works.
> I managed to get it all working, 
> 
> Since I was on my way to something a bit slightly more complex ...one last 
> Question
> 
> What if I would like to:
> 
> Class RelationType(Base):
>       __tablename__           = 'relationtypes'
>       Id                                      = Column(Integer, 
> primary_key=True)
>       ChildRelationType       = Column(Unicode(20))
>       ParentRelationType   = Column(Unicode(20))
> 
> Some data for information:
> 
> Id                                    ChildRelationType                       
> ParentRelationType
> 1                                     "Parent"                                
>                 "Child"
> 2                                     "Supplier"                              
>         "Customer"
> 
> I will put additional logic to print Mother - Son, in Fact this is not really 
> what I am doing but I feel the need to make it a bit more understandable for  
> the MailingList
> At the end it will be something with molecules, not really interesting and 
> easy to understand.
> 
> 
> 
>> 
>> relation_table = Table('relation', Base.metadata,
>>   Column('rid', Integer, ForeignKey('affiliations.id'), primary_key=True),
>       Column('RelationType', integer, ForeignKey('relationtypes.Id')),
>>   Column('lid', Integer, ForeignKey('affiliations.id'), primary_key=True),
>> )
> 
> 
> Adding this column to the relation_table does not make a difference but....
> 
> Somehow I need to get a reference to the "type of relation"
> Setting the relation_type MUST be done whilst "Appending" it. i.e. every 
> relation between "Affiliates" must have a reason
> 
> 
> Is this possible or is there another way to do such a thing, I feel logically 
> it should be part of the relation_table.
> 
> Thank you Again for your great help. I'm Not really good in Database stuff, 
> but did/do a lot on the mod_python/mod_wsgi lists so I know about the huge 
> amounts of time spent....

You then turn the relation_table into an association object.  A declarative 
example is in 
http://www.sqlalchemy.org/docs/orm/relationships.html#association-object .

The "Relation" class will have a relationship() to "child", or similar (and 
probably "parent" as a backref).  Intercept the "set" event on this 
attribute(s) and use that to establish the desired "RelationType" value.   This 
interception can be done with the @validates decorator:  
http://www.sqlalchemy.org/docs/orm/mapper_config.html#simple-validators .


-- 
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