There might/probably is a better way to achieve what I am trying to
achieve.  Let me explain:

I've got a typical Customer/Order/Orderdetail relation.

merge() appeals to me because it does most all the same work I'd
otherwise need to do.  I get passed to me an "Order" object will the
customer object and the details objects.

Now, when I run merge, if there are any changes to the customer, these
get automagically updated.  If a line is missing, it is automatically
deleted.  If a new line exists, it is automatically inserted.

session.merge() does all this.

However, the above is a bit too simplified.  If the Customer's primary
key is not supplied, I need to create this in a special way (not from
a sequence) and then insert it.  In this case, I don't even want merge
to *attempt* to get() from the database first because if, by chance,
the customerid I construct is already in use, I do not want to
accidentally update that record, I *want* the database to complain on
the INSERT that the primary key already exists.

Same with the order and orderdetails.  If an orderdetail is supplied
to me without a primary key, I would like to choose the next
appropriate primary key and INSERT for that particular record, so that
(if something is messed up or a race condition exists with another
instance of the program choosing the same primary key) I get a
database error instead of merge() accidentally updating that other
record.

I don't want to give up the benefits of merge() by programmatically
figuring this out myself.  In other words, if I add() to the session
all the lines I *know* should be INSERTs then I need to work out all
the other magical things merge() is doing for me (like deleting
missing lines).

I tried seeing what would happen if I just called session.add() for
the cases of an INSERT and then calling session.merge() for the entire
cascading order(customer/details), but when I tried that I got:
New Instance conflicts with persistent instance type error.

I assume this means: you tried to session.add() and object and now I
just looked up this same instance during the session.merge().

So instead, I was wondering if there is a way to mark an object in
such a way that when session.merge() recursively hits this object, it
knows to not bother with the fetch but instead does a session.add()?

Otherwise, is there a better approach to what I am trying to
accomplish?

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