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?
On Thursday, February 19, 2015 at 5:29:56 PM UTC+1, Michael Bayer wrote:
Maurice Schleußinger <m.schle...@gmail.com> 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 sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.
There may be better ways if your backend supports it. E.g. if you’re using a recent version of PostgreSQL, then diagnostics information is attached to the exception being thrown by psycopg2 (which is available under the .orig property of the SQLAlchemy exception). See the documentation of psycopg2.extensions.Diagnostics and the documentation of libpq. I don’t know if there are enough details in the diagnostics to support your use case.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to
sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.