SF Markus Elfring <elfr...@users.sourceforge.net> wrote:

>>> 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(),
> 
> Yes. - It seems that this method was used in my use case.
> 
> 
>> which receives the full set of records in one batch before any
>> communication with the database is established.
> 
> Can it happen then that an error will be reported for a single SQL statement
> which was submitted within an unit of more database commands?
> 
> 
>> SQLAlchemy doesn’t have access to at what point each individual series
>> of parameters are invoked as the interface is too coarse-grained.
> 
> Do you know any attempts to make the affected error reporting more precise?


Here are the two options we have for invoking a statement:

try:
    cursor.execute(stmt, params)   # single row
except IntegrityError:
   # …

try:
   cursor.executemany(stmt, [params, params, params, params, …])   # many rows
except IntegrityError:
   # …

There is no way to use the second form while being able to record the moment 
each parameter set is used, unless the DBAPI itself provides additional hooks 
for logging at this level.   However, this logging would defeat some of the 
purpose of executemany(), which is that of processing many parameter sets at 
maximum speed.

The SQLAlchemy Session tries to use executemany() as often as it can within a 
flush() procedure; it can be used any time there are more than one row to be 
INSERTED where we already have the primary key value available.

If you’d like to operate on individual rows, I guess I wasn’t specific enough 
from my instruction to use SAVEPOINT, you should flush individually:

for obj in all_my_objects:
        session.add(obj)
        try:
            with session.begin_nested():
                session.flush()
        except IntegrityError:
            # deal with this error, don’t add obj, or however it is you intend 
to deal with existing rows


    



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

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

Reply via email to