> On Feb 23, 2015, at 8:49 AM, Maurice Schleußinger <[email protected]> 
> wrote:
> 
> Thanks for the reply!
> 
> Since I create objects concurrently I can not predict and therefore not 
> pre-select all rows which could possibly provoke IntegrityErrors. 
> 
> On the other hand Session.merge() seams to provide the functionality which 
> could resolve my problem. In my setup many processes create a number of 
> objects which could occur multiple times (in other words throw an 
> IntegrityError on commit). That can happen in one process (which I can handle 
> otherwise), in between multiple processes and between a process and the 
> consistent state in the database (which is my main problem ATM). 
> 
> I just read the corresponding part in the SQLAlchemy docs. So if I use 
> Session.merge() with the load=True argument (instead of Session.add()) the 
> session should create the corresponding object if it does not exists, avoid 
> duplicates within one session and also update an existing entry in the 
> database?
> 
> Also it seems that merging only works for primary keys. So if I had the 
> problem with an unique key, I still would have to parse the exception, right?

Ah ok, yes session.merge() works this way, but it does emit a SELECT.   If you 
were looking to emit less SQL I was referring to an operation like MySQL's 
REPLACE statement.   

But if merge works for you then stick with that.   You can write your own 
function that does something similar for other fields that are not the PK.    
The recipe below is one way to do this.

https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/UniqueObject



> 
>> On Thursday, February 19, 2015 at 5:29:56 PM UTC+1, Michael Bayer wrote:
>> 
>> Maurice Schleußinger <[email protected]> wrote: 
>> 
>> > Is there no other way? 
>> > 
>> > http://stackoverflow.com/questions/27635933/how-can-i-know-which-fiels-cause-integrityerror-when-inserting-into-a-table-with/27884632#27884632
>> >  
>> > 
>> > Parsing an exception with regex just doesn't feel right… 
>> 
>> 
>> The two other ways are that you can pre-select the rows, or use a MERGE / 
>> upsert approach (you’d have to roll that yourself).
> 
> -- 
> You received this message because you are subscribed to the Google Groups 
> "sqlalchemy" group.
> To unsubscribe Sent from my iPhone
> 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/d/optout.

-- 
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/d/optout.

Reply via email to