Hi,
I am trying to build an attribute_mapped_collection reference from
table people (Mapped class is called Person). However, I would like to
get a list of entities for each key.
I have the following tables with the relevant PK and FK listed
Person:
- id
PersonToAddress:
- id
- person_id
- address_id
role # this is the mapped special key
Address:
- id
to establish a relationship i do the following (only the relationships
are included in the listing)
class PersonToAddress:
person = relationship( __table_to_classnames__['people'],
backref=backref('people_to_addresses',
collection_class=attribute_mapped_collection("role")))
class Person:
addresses_by_role =
association_proxy('people_to_addresses','address',creator = lambda
k,v:PeopleToAddress(role=k,address=v))
Now querying yields this result:
p = Session.query(Person).get(id=1)
print p.addresses_by_role
{u'home': <Address object at 0x29568d0>, u'work': <Address object at 0x2a3eb90>}
I would like to get a list as value for the dict, such that i can
assign more than one entity to any one key. The output should look
like this:
{u'home': [<Address object at 0x29568d0>,<Address object at ...>] ,
u'work': [<Address object at 0x2a3eb90>]}
Now in the database whenever i set a new value for a key(=role), the
entry in PersonToAddress' table is replaced (not added). This is
consistent with having a 1-key to 1-value mapping. Can I however
change the behaviour in such a way that more than one Addresses are
allowed for one Person using the same key(=role in this example)?
I should note that i tried supplying the uselist=True parameter in the
backref argument to PersonToAddress.person. This, however does
nothing.
Adding uselist=True to the parameters of the relationship (as opposed
to the backref) does create a list in both the backref and
addresses_by_role's values. The list, however only contains an
element, and if a new one is added, the entry in the db is
changed.Still only 1 element of the list is ever present in
PersonToAddress' table.
Am i overlooking something in the way attribute_mapped_collection
should be used ?
Paul
--
[email protected]
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.