Hi,
I'd like to know how to implicitly access attributes in many to many
relations. Let's assume the following existing tables:

person_table=Table('person',metadata,
Column('personid',Integer,primary_key=True),
Column('name',String(50))

address_table=Table('address',metadata,
Column('addressid',Integer,primary_key=True),
Column('street',String(50))

r_pers_addr_table=Table('r_pers_addr',metadata,
Column('r_pers_addrid',Integer,primary_key=True),
# Relation attribute that denotes if an address is the primary location
of a person
Column('prim_addr',String(1), default='N'),
Column('personid',Integer,ForeignKey('person.personid'),
Column('adresseid',Integer,ForeignKey('adresse.adresseid'))

Let's assume that the above scenario is not very optimal, as this many
to many relation can be replaced by a one to many relation, where
"prim_addr" and "personid" is stuffed into the "address_table".
However, currently I have to deal with this data model but in the
future, it will be migrated - of course this migration should not
result in an extensive code-review.

So, what I'd like to have is an object "person" and "address"
(instances of classes "Person" and "Address", where I can access/store
attributes like this:

person.name='abc'
person.address[0].street='xyz'
person.address[0].prim_addr='N'

But how would I create a mapper in this case? I could do something like
this:
-----------
mapper(Person, person_table, properties = dict(
    address = relation(Address, secondary=r_pers_addrid_table)))
-----------
This would allow access like "person.address[0].street" but not
"person.address[0].prim_addr.

The following mappers allows full access:
-----------
mapper(Person, person_table, properties={
    'r_pers_addr':relation(R_pers_addr)
    }
)

mapper(R_pers_addr, r_pers_addr_table,
    properties={
        'address' : relation(Address)})
-----------
But now I can not directly address the street, e.g. I have to do:
person.r_pers_addr[0].address.street
But I can access the relation attribute:
person.r_pers_addr[0].prim_addr

What I could do is create specific properties in my class "Person" for
"street" and "prim_addr" but that is quite complicated, moreover I have
to adapt my class in case of a data structure migration. Maybe I could
also do the following:
-----------
mapper(Person, person_table, properties = dict(
    address = relation(Address, secondary=r_pers_addrid_table))
mapper(Address, address_table, properties = dict(
    r_pers_addr=relation(R_pers_addr))
-----------
So I could do: person.address[0].street and
person.address[0].r_pers_addr.prim_addr - but once again I cannot do a
person.address[0].prim_addr

My current data model which I have to access, is full of such "relation
attributes", moreover this scenario is probably quite common, so
perhaps there's a good solution?

Best Regards,
Hermann


--~--~---------~--~----~------------~-------~--~----~
 You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to sqlalchemy@googlegroups.com
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