Michael Bayer wrote:
Specifically, the following:

- change the primary key of the instance by deleting it so that the dataabse 
assigns a new primary key?

that wont work. you need to take the object and call make_transient() on it, re-add it to the session.

Where is make_transient documented? (failing that, where do I import it from?)

> the latest you could do this would be sessionextension.before_flush().

How do I know what instances I need to worry about in before_flush?
http://www.sqlalchemy.org/docs/05/reference/orm/interfaces.html#sqlalchemy.orm.interfaces.SessionExtension.before_flush implies is `instances` list passed can't be relied upon...

- do a query to update an existing row? (where would I get the session from?)

you can do that, if you use the given connection.   session is from 
object_session(instance).

So the session returned by object_session will always be connected to the connection provided in the `connection` parameter?

- change the row the instance corresponds to and somewhow return a "new replacement 
instance"?

not sure what that means

That's this bit from my previous example:

>>> session.add(x)
>>> session.commit()
>>> x.id, x.name, x.value, x.valid_from, x.valid_to
1, 'foo', 100, 2010-06-29 09:00, None

>>> x.value = 200
>>> session.commit()
>>> x.id, x.name, x.value, x.valid_from, x.valid_to
2, 'foo', 200, 2010-06-29 09:05, None

>>> for thing in session.query(MyThing).all():
...   print x.id, x.name, x.value, x.valid_from, x.valid_to
1, 'foo', 100, 2010-06-29 09:00, 2010-06-29 09:05
2, 'foo', 200, 2010-06-29 09:05, None

`x` used to correspond to the row with id of 1, but now corresponds to the row with the id of 2. That's the effect I'm looking to achieve.

so far every versioning thing I've written is more appropriate for 
before_flush() rather than before_insert/update..   its likely easier to keep 
thing there since you have no restrictions.

Are there good examples of before_flush() implementations to look at anywhere?

> Also, feel free to use another recipe for versioning based on the model you'd like. I'm using a recipe right now that has a "primary" object which points to several "temporal" objects. When you say parent.some_child.foo = 'bar', the before_flush() extension checks for real changes using session.is_modified(),

The model we're looking for is roughly:

- only primary objects in one table
- the valid_from and valid_to form a linked list of version validity
- only certain column changes cause a new version to be created (eg: if the 'notes' column changes, we don't want to create a new version; would session.is_modified() still help with that?)

then uses the aforementioned make_transient() to convert the UPDATE into an 
INSERT.

okay, so we would need to cause an update or the original row, an insert of the new row and then mutate the object to be attached to the new row, and no longer attached to the updated row, if that makes sense?

Any ideas gratefully received! :-)

cheers,

Chris

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

Reply via email to