Stakes book and condition book transformations create duplicate purse distribution data
Resolved 💬 2 comments Opened Jan 19, 2026 by rcsmith27 Closed Feb 13, 2026
Summary
When running core_planned_race_stakes_book after core_planned_race_condition_book, a unique constraint violation occurs on the planned_race_purse_distribution table.
Error
psycopg2.errors.UniqueViolation: duplicate key value violates unique constraint "uq_planned_race_purse_distribution"
DETAIL: Key (planned_race_id, finish_position)=(7573, 4) already exists.
Root Cause
Both transformation assets can create purse distributions for the same planned race:
core_planned_race_condition_bookcreates planned races and their purse distributions from condition book PDFscore_planned_race_stakes_bookattempts to insert purse distributions that may reference the sameplanned_race_idvalues
The stakes book asset deletes races where stakes_book_source_file IS NOT NULL before inserting, but this doesn't handle cases where:
- A race was created by condition books (has
planned_race_card_id) - The same race also appears in stakes books and tries to add purse distributions
Affected Files
systems/equibase_web/src/equibase_web/stakes_core/assets.py-core_planned_race_stakes_booksystems/equibase_web/src/equibase_web/stakes_core/sql/stakes_books.sql-insert_purse_distributions_from_stakes_books
Proposed Solutions
- Add ON CONFLICT handling to the purse distribution INSERT statements to handle duplicates gracefully (UPDATE or IGNORE)
- Improve race matching logic to detect when a stakes book race already exists from condition books and update rather than create duplicates
- Clear related child data before inserting - when stakes book processing finds matching races, clear their existing purse distributions first
Reproduction Steps
- Truncate planned race tables
- Run
raw_condition_bookasset - Run
core_planned_race_condition_bookasset - Run
core_planned_race_stakes_bookasset → Error occurs
🤖 Generated with Claude Code
This issue has 2 comments on GitHub. Read the full discussion on GitHub ↗