Skip to content

Commit 35377b5

Browse files
committed
Fix issues that game didn't end well
Resolves #22 Signed-off-by: Seonghyeon Cho <seonghyeoncho96@gmail.com>
1 parent a53211c commit 35377b5

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

packages/server/src/rooms/game-room.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,12 @@ export class GameRoom implements DurableObject {
608608
});
609609
}
610610
const db = getDb(this.env.DB);
611+
// games row must land before any FK-dependent insert (game_participants,
612+
// daily_attempts both reference games.id). D1 dispatches concurrent
613+
// prepared statements independently, so Promise.all-ing all of them
614+
// races the dependents past the parent and trips SQLITE_CONSTRAINT.
615+
await db.insert(games).values(gameRow).run();
611616
const writes: Promise<unknown>[] = [];
612-
// games row is always inserted (any mode, any participant set) so daily
613-
// attempts and future analytics have a stable join key.
614-
writes.push(db.insert(games).values(gameRow).run());
615617
if (participantRows.length > 0) {
616618
writes.push(db.insert(gameParticipants).values(participantRows).run());
617619
}

0 commit comments

Comments
 (0)