hey folks -

I just checked in the "backref" feature.

The manytomany unit test looks like this:

Place.mapper = mapper(Place, place)
Transition.mapper = mapper(Transition, transition, properties = dict( inputs = relation(Place.mapper, place_output, lazy=True, backref='inputs'), outputs = relation(Place.mapper, place_input, lazy=True, backref='outputs'),
            )
        )

and thats the whole mapper ! it creates the 'inputs' and 'outputs' properties on the Place.mapper as well. (hm, is "place_output" supposed to attach to the "outputs" property on the Place object? thats a pretty complex structure...) you can append rows to 'inputs' or 'outputs' on either type of object and they show up on the corresponding list on the appended object.

one interesting thing, is when you append items to the Transition's inputs, and then it automatically appends items to the Place's inputs as well, the eventual "commit" was inserting two association rows, since it was picking up two additions, even though they are in fact the same thing.

there were two ways around this; one was to make the "backreference" handler append the secondary rows without triggering a "history" event. the only problem with that is if you try to "roll back" all the histories of the objects, that wouldnt work, since new stuff was appended with no history. So the way i did it is when you have this two way "backreference" thing with a many to many relationship, only one of the mappers actually logs its changes when you do a commit(). this has the effect of consolidating the insert/update statements as well.

Also another unit test in the objectstore package to test one-to-many/ many-to-one. below, we attach the 'user' to the 'address', and test that the user's list of addresses gets updated:

class User(object):pass
class Address(object):pass
am = mapper(Address, addresses)
m = mapper(User, users, properties = dict(
       addresses = relation(am, backref='user'))
)

u = User()
a = Address()
a.user = u
self.assert_(u.addresses == [a])
objectstore.commit()

- mike







-------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc. Do you grep through log files
for problems?  Stop!  Download the new AJAX search engine that makes
searching your log files as easy as surfing the  web.  DOWNLOAD SPLUNK!
http://ads.osdn.com/?ad_id=7637&alloc_id=16865&op=click
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to