Maintain data consistency by using appropriate database operations and ensuring transactional integrity when updating related fields. Choose the right ORM methods (upsert vs create) to handle potential duplicates, and ensure that updates to related fields across multiple tables happen atomically to prevent data drift.
Key practices:
upsert()
instead of create()
when records might already exist to avoid UniqueViolationErrorExample of proper upsert usage:
# Instead of create() which can fail on duplicates
await self.prisma_client.db.litellm_managedobjecttable.upsert(
where={"object_id": object_id},
data=data,
create=data
)
# Correct schema field names in includes
include={"end_users": True} # Not "litellm_endusertable"
This prevents UniqueViolationError exceptions and maintains referential integrity across related database entities.
Enter the URL of a public GitHub repository