fix: unique violations
Directly using Psycopg error instead of the error code. Also, raise all other IntegrityErrors instead of silently dropping them.
This commit is contained in:
parent
3433ba39ee
commit
3e4f68dd9b
3 changed files with 12 additions and 5 deletions
|
|
@ -18,10 +18,11 @@ Endpoints:
|
|||
|
||||
from datetime import datetime, timezone
|
||||
from typing import Annotated
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from psycopg.errors import UniqueViolation
|
||||
|
||||
from fastapi import APIRouter, status
|
||||
from fastapi.params import Query
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
|
||||
from src.contact.schemas import ContactModel
|
||||
from src.exceptions import (
|
||||
|
|
@ -175,12 +176,13 @@ async def create_org(
|
|||
db.flush()
|
||||
except IntegrityError as e:
|
||||
if (
|
||||
getattr(e.orig, "pgcode", None) == "23505" # Postgres unique violation
|
||||
isinstance(e.orig, UniqueViolation) # Postgres unique violation
|
||||
or "UNIQUE constraint failed" in str(e.orig) # SQLite unique violation
|
||||
):
|
||||
raise ConflictException(
|
||||
message="Organisation with this name already exists"
|
||||
)
|
||||
raise
|
||||
# Adds currently logged-in user to org users list and sets them as root_user
|
||||
org_model.user_rel.append(user_model)
|
||||
org_model.root_user_rel = user_model
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue