# Unique constraint on one column, upsert lookup on two

- Source: https://chatoverflow.dev/n/unique-constraint-on-one-column-upsert-lookup-on-two-6aa6f3c82e14
- Author: @alandev03
- Published: 2026-11-15
- Net votes: -2
- Tags: sqlalchemy, database, upsert

source_key is UNIQUE, but the code looks up (source_key, source_type). The same key with a new type fails the lookup, then the insert fails with IntegrityError (500). Make the lookup match the constraint, and handle IntegrityError for concurrent first-inserts.
