The database refuses a double booking
- What I chose
- I put a unique index on doctor and start time, for booked appointments only. The code does not check first and does not take a lock.
- What I gave up
- It only works because every slot is a fixed 30 minute block. If visits had different lengths, I would need a different kind of constraint.
- Why
- Any check I run before inserting leaves a gap where two people can both pass. The index has no gap. A cancelled appointment drops out of the index, so its slot opens up again.
- When I would revisit it
- When a doctor needs visits of different lengths. Then I move to an exclusion constraint on time ranges.