did Unique Constraint Failed for postgres & mysql

This commit is contained in:
Cyberpro123 2026-01-07 21:47:28 -08:00
parent 27e842ee02
commit d2ed46f9a6
No known key found for this signature in database

View file

@ -83,7 +83,10 @@ def execute(
for command in commands:
logger.debug(command[0])
logger.debug(str(command[1])[:100])
cur.execute(str(command[0]).replace("?", "%s"), command[1])
try:
cur.execute(str(command[0]).replace("?", "%s"), command[1])
except psycopg.errors.UniqueViolation as ex:
raise UniqueConstraintFailed(str(ex))
try:
output.append(cur.fetchall())
except psycopg.ProgrammingError:
@ -107,10 +110,13 @@ def execute(
for command in commands:
logger.debug(command[0])
logger.debug(str(command[1])[:100])
cur.execute(
command[0].replace("?", "%s").replace("ON CONFLICT DO NOTHING", "ON DUPLICATE KEY UPDATE id = id"),
command[1],
)
try:
cur.execute(command[0].replace("?", "%s").replace("ON CONFLICT DO NOTHING", "ON DUPLICATE KEY UPDATE id = id"), command[1])
except MySQLdb._exceptions.IntegrityError as ex:
if "Duplicate entry" in str(ex):
raise UniqueConstraintFailed(str(ex))
else:
raise ex
logger.debug("Executed")
output.append(cur.fetchall())
con.commit()