ponyorm / pony

Pony Object Relational Mapper
Apache License 2.0
3.63k stars 245 forks source link

Can't catch pony.orm.core.TransactionIntegrityError #676

Open thmsklngr opened 1 year ago

thmsklngr commented 1 year ago

Hi,

I'm currently out of any idea. I have some issues with external data, which is not so dramatic for my data I want, but it causes a TransactionIntegrityError and I can't catch it, regardless whatever I do.

Example:

try:
    MyModel(value1=value1, value2=value2)
except pony.orm.core.TransactionIntegrityError as exc:
    print(f'Error: {exc}')
else:
    db.flush()
    db.commit()

From my point of view this syntax is correct, however, the exception is thrown anyway, it's not caught by the try/except block. I also exchanged pony.orm.core.TransactionIntegrityError with Exception for more general purpose, no effect.

Am I doing something wrong or what may be the reason?

Many thanks in advance, regards, Thomas

erik-megarad commented 1 year ago

Is the exception being raised from inside the else block? Try this

try:
    MyModel(value1=value1, value2=value2)
    db.flush()
    db.commit()
except pony.orm.core.TransactionIntegrityError as exc:
    print(f'Error: {exc}')