Skip to content

Instantly share code, notes, and snippets.

@daaniam
Created April 30, 2024 03:22
Show Gist options
  • Save daaniam/1389ecfb8aeac6c04926e50f037e68dc to your computer and use it in GitHub Desktop.
Save daaniam/1389ecfb8aeac6c04926e50f037e68dc to your computer and use it in GitHub Desktop.
SQLAlchemy asyncpg
"""
Just a note where to find original asyncpg exception object wrapped by SQLAlchemy
"""
try:
created_record = await db.scalar(insert(Model).values(**data_in.model_dump()).returning(Model))
except IntegrityError as err:
if isinstance(err.orig.__cause__, UniqueViolationError):
raise RecordAlreadyExists()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment