SF Markus Elfring <elfr...@users.sourceforge.net> wrote:
>> SQLAlchemy sends to the log the statement and parameters it is to send >> to the DBAPI cursor.execute() method, *before* it actually does so. >> This so that if the DBAPI throws an exception, as is the case here, >> one can see what instructions were sent to it which were the immediate >> cause of this error. > > Thanks for your explanation. > > Do I need to consider any more fine-tuning for my database session? > > >> The mechanism of a UNIQUE constraint is that this is a database-level >> construct, so the backend database is tasked with checking this >> this condition and reporting on it at statement execution time. > > Should I get the exception "sqlalchemy.exc.IntegrityError" directly > after I attempted to insert a second record set with unique attributes > into a SQLite table? I don’t have a stack trace here to see what the nature of the issue is but it is likely that the INSERT is proceeding using DBAPI executemany(), which receives the full set of records in one batch before any communication with the database is established. SQLAlchemy doesn’t have access to at what point each individual series of parameters are invoked as the interface is too coarse-grained. The two general techniques for dealing with unique constraints are to either SELECT ahead of time the rows that you know to be dealing with into a collection, such that you can check within this collection ahead of time for the existing row before proceeding, or invoking the INSERT of rows one at a time, catching each IntegrityError inside of a SAVEPOINT (http://docs.sqlalchemy.org/en/latest/orm/session_transaction.html#using-savepoint describes the Session’s API for SAVEPOINT). Note that the SQLite driver has a bug with SAVEPOINT which you need to apply the technique at http://docs.sqlalchemy.org/en/latest/dialects/sqlite.html#pysqlite-serializable in order to overcome. > > Regards, > Markus -- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.