Skip to content

Commit 7ae4d92

Browse files
fastapi-sqlalchemy-pg-catalog/init.sql: clarify when NOT EXISTS actually matters
Copilot noted /docker-entrypoint-initdb.d scripts only run on first database initialization, so the previous comment's framing ('re-run against an existing volume') was misleading. Reworded to call out the actual scenario: this is a single-shot insert on a clean volume, and NOT EXISTS is defensive coverage for stale-volume reuse. Signed-off-by: Akash Kumar <meakash7902@gmail.com>
1 parent bd9cfc0 commit 7ae4d92

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

fastapi-sqlalchemy-pg-catalog/init.sql

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,17 @@ CREATE TABLE IF NOT EXISTS project (
1212
name VARCHAR(100) NOT NULL
1313
);
1414

15-
-- Idempotent seed. `ON CONFLICT DO NOTHING` would only help with a
16-
-- UNIQUE/EXCLUSION constraint on name, which the SQLAlchemy model
17-
-- doesn't declare; use NOT EXISTS so re-running this script against
18-
-- an existing volume doesn't duplicate the row.
15+
-- Seed the table.
16+
--
17+
-- Postgres only runs scripts under /docker-entrypoint-initdb.d on
18+
-- *first* database initialization (empty data dir), so on a clean
19+
-- container this is a single-shot insert and `ON CONFLICT` /
20+
-- `NOT EXISTS` wouldn't normally matter. The `NOT EXISTS` guard is
21+
-- defensive belt-and-suspenders for the degenerate case where the
22+
-- compose stack reuses a stale Postgres data volume that already
23+
-- carries the seed row — it keeps the script idempotent without
24+
-- requiring a UNIQUE constraint on project.name (which the
25+
-- SQLAlchemy model doesn't declare).
1926
INSERT INTO project (name)
2027
SELECT 'seed'
2128
WHERE NOT EXISTS (SELECT 1 FROM project WHERE name = 'seed');

0 commit comments

Comments
 (0)