Add unique constraint to prevent duplicate (author, chapter, page) submissions
Adds a PostgreSQL partial unique index on (author, chapter, page) where all three fields are non-null, and returns HTTP 409 when a duplicate is detected. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -45,6 +45,13 @@ def init_db():
|
||||
cur.execute("""
|
||||
CREATE INDEX IF NOT EXISTS ocr_jobs_submitted_at_idx ON ocr_jobs(submitted_at DESC)
|
||||
""")
|
||||
# Unique constraint: prevent duplicate (author, chapter, page) submissions.
|
||||
# Applies only when all three fields are non-null.
|
||||
cur.execute("""
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS ocr_jobs_author_chapter_page_unique
|
||||
ON ocr_jobs (author, chapter, page)
|
||||
WHERE author IS NOT NULL AND chapter IS NOT NULL AND page IS NOT NULL
|
||||
""")
|
||||
conn.commit()
|
||||
print("Database initialized.")
|
||||
except Exception as exc:
|
||||
|
||||
Reference in New Issue
Block a user