Review request

Ceramics studio class booking in Lovable, we keep getting double bookings

Solved · 9 views · asked by noor_builds · edited

Repo or live app

github.com/noor-haddad/kiln-booking

Unsure about: OtherStructure

Eight wheels, classes of two hours. Lovable checks for a conflicting booking first and then inserts. Twice this month two people got the same wheel at the same time, both on a Sunday evening when the site is busiest. The check looks correct to me, which is the confusing part.

Comment

2 answers

Marked as helpful by the asker
mira_dev · edited

The check is correct and still wrong. Between your select and your insert another request runs the same select and also sees no conflict. Both inserts succeed. It only shows up under load, which is why you see it on Sunday evenings.

Let the database decide instead of your code:

create extension if not exists btree_gist;

alter table bookings add column during tstzrange
  generated always as (tstzrange(starts_at, ends_at, '[)')) stored;

alter table bookings add constraint no_overlap
  exclude using gist (wheel_id with =, during with &&);

Now the second insert fails with a unique violation no matter how the requests interleave. Catch code 23P01 and show "that slot just went". Keep your pre-check for the nice error message, but stop trusting it.

Comment
olu_backend · edited

One practical note on top of the exclusion constraint: cancellations. If you soft-delete by setting status = 'cancelled', the cancelled row still occupies the range and blocks rebooking. Add a where clause to the constraint so it only applies to live rows:

exclude using gist (wheel_id with =, during with &&)
  where (status = 'confirmed')
Comment