Scenario · target: seed postgres from openapi spec
Seed Orders API from OpenAPI: seed postgres from Guide
Developer wants a repeatable command that turns an OpenAPI file into loadable Postgres seed rows.
Direct answer
SchemaSeed (a Node.js CLI) turns an OpenAPI 3.x document into a PostgreSQL seed script for Orders API in which every foreign key resolves to a row that was actually inserted.
This holds for OpenAPI 3.0 and 3.1 with local $ref only; remote $ref URLs and oneOf polymorphism are reported as warnings, and the output targets PostgreSQL 13 or newer.
Run npx --yes @wenlu8848/schemaseed generate --input [the fixture that ships with this page] --out ./seed --formats sql --seed 42 and the script contains 3 CREATE TABLE statements followed by the rows in dependency order, inside one BEGIN/COMMIT pair.
Data as of 2026-11-18 · source: 0 Stack Overflow questions, 14 Hacker News stories, @hey-api/spec-types at 19,345,984 monthly downloads · negative-case messages reproduced by executing the fixture against a local SQL engine (Node.js v24.22.0, engine 3.53.4) at 2026-11-18T11:53:54.233Z
What the generated script actually contains — Orders API
Running the generator over the Orders API fixture produces 3 tables and 13 columns in one transaction. The parent table is written first, the child rows reuse the parent key that was just inserted, and the whole file is wrapped in a single BEGIN/COMMIT pair, so a failure halfway through rolls the database back instead of leaving orphaned rows behind. The constraint count that the generator honours is 19, which is the number of primary key, foreign key, NOT NULL and CHECK clauses it read out of the schema rather than invented. The enum, range and length declarations in the same schema are read out as well, and the value that breaks each of them ships as a negative case.
The failure you are trying to avoid
Hand-written INSERT statements for Orders API fail at exactly one point: the first child row whose parent does not exist yet. The engine reports it as foreign_key_violation with SQLSTATE 23503, and the message text on the reference engine was "FOREIGN KEY constraint failed". The generator removes that failure by ordering the tables topologically before it writes a single row, so orders is never written before the row it points at.
Boundary cases that come with the seed
The seed is paired with a negative fixture set. For Orders API, the range probe on orders.total_cents uses the value -1, which the constraint "total_cents" >= 0 rejects. The engine produced "CHECK constraint failed: orders_total_cents_check" for that insert. A test suite that only checks the happy path never notices that the API accepts a value it should refuse.
What the output looks like in review
The output is plain SQL, not a binary blob, so it diffs cleanly in code review. Every INSERT in the Orders API fixture names its columns explicitly rather than relying on column order, which means the file survives someone adding a column to the schema. The seed file creates the tables users, orders, order_items, and each of those tables appears exactly once per seed file.
When a different tool fits better
If the Orders API fixture has a single table with no foreign key, a per-schema faker is enough and this page is not the fastest route. The generator earns its place when a schema has more than one table that reference each other, when the output has to be SQL rather than JSON, or when the test suite needs the invalid instances as well as the valid ones.
| Failure class | Column | SQLSTATE | Error name | Engine message (verbatim) |
|---|---|---|---|---|
| Missing required field | users.email | 23502 | not_null_violation | NOT NULL constraint failed: users.email |
| Out-of-contract value | orders.total_cents | 23514 | check_violation | CHECK constraint failed: orders_total_cents_check |
| Dangling foreign key | orders.user_id | 23503 | foreign_key_violation | FOREIGN KEY constraint failed |
| Table | Referenced by | Written |
|---|---|---|
| users | orders | after orders |
| orders | order_items | after order_items |
| order_items | — | first, no dependencies |
npx --yes @wenlu8848/schemaseed generate --input ./orders.openapi.yaml --out ./seed --formats sql --count 10 --seed 42
-- Generated by schemaseed.
-- seed: 42
-- rows per table: 10
-- Foreign key values are taken from the rows of the referenced table.
BEGIN;
CREATE TABLE IF NOT EXISTS "users" (
"id" UUID,
"email" TEXT NOT NULL,
"created_at" TIMESTAMPTZ NOT NULL,
CONSTRAINT "users_pkey" PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "orders" (
"id" UUID,
"user_id" UUID NOT NULL,
"status" TEXT NOT NULL,
"total_cents" INTEGER NOT NULL,
"placed_at" TIMESTAMPTZ NOT NULL,
CONSTRAINT "orders_pkey" PRIMARY KEY ("id"),
CONSTRAINT "orders_status_check" CHECK ("status" IN ('placed', 'paid', 'shipped', 'cancelled')),
CONSTRAINT "orders_total_cents_check" CHECK ("total_cents" >= 0)
);
CREATE TABLE IF NOT EXISTS "order_items" (
"id" UUID,
"order_id" UUID NOT NULL,
"sku" TEXT NOT NULL,
"qty" INTEGER NOT NULL,
"unit_price_cents" INTEGER NOT NULL,
CONSTRAINT "order_items_pkey" PRIMARY KEY ("id"),
CONSTRAINT "order_items_qty_check" CHECK ("qty" >= 1),
CONSTRAINT "order_items_unit_price_cents_check" CHECK ("unit_price_cents" >= 0)
);
DO $schema_seed$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'orders_user_id_fkey') THEN
ALTER TABLE "orders" ADD CONSTRAINT "orders_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users" ("id");
END IF;
END
$schema_seed$;
DO $schema_seed$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'order_items_order_id_fkey') THEN
ALTER TABLE "order_items" ADD CONSTRAINT "order_items_order_id_fkey" FOREIGN KEY ("order_id") REFERENCES "orders" ("id");
END IF;
END
$schema_seed$;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('08500a62-07e7-4342-a70f-3e3d1de0d2ea', 'gus.grant869@mail.example', '2025-06-19T16:15:12.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('4f0b1e9c-9d5c-4017-8c1f-6d283129e2c9', 'lena.bianchi562@example.org', '2022-06-27T03:35:20.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('30f4d746-2479-4627-b307-9cf71163dd62', 'hana.fontaine467@example.org', '2020-06-12T21:22:12.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('4036a8cd-2761-484c-a461-6a7ee67eeb5a', 'lena.jansen818@example.net', '2020-03-15T08:44:18.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('30b5e98c-258a-4465-8d2e-e773c7f35318', 'nia.engel29@example.org', '2024-04-05T04:30:57.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('d8c34b2c-4b52-4b19-a8cc-24d54ddf0d83', 'fay.petrov98@mail.example', '2023-10-10T04:38:41.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('beccedb9-2b7d-4860-bff2-44939d363f79', 'nia.costa96@example.net', '2025-09-25T02:04:31.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('d46985f5-4729-4ba5-b54c-412e72a3996d', 'ada.holt610@example.org', '2023-04-24T04:11:41.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('f52b77c2-00db-492c-9c79-930e96f7a90d', 'pia.novak742@mail.example', '2020-10-15T23:43:36.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "users" ("id", "email", "created_at")
VALUES ('888d989a-32eb-437c-8a53-0c8e2e8e5e12', 'pia.engel914@test.dev', '2023-12-06T12:48:26.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('fca703d1-9e25-450e-b57d-c3771178e8c1', '4f0b1e9c-9d5c-4017-8c1f-6d283129e2c9', 'shipped', 337, '2024-10-20T23:58:32.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('2a7c2149-b84e-4317-a47b-d70dd75c9ee9', '4f0b1e9c-9d5c-4017-8c1f-6d283129e2c9', 'cancelled', 137, '2022-01-26T05:16:18.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('3a357283-6d2c-4d5d-90d3-e605d663e7dd', '08500a62-07e7-4342-a70f-3e3d1de0d2ea', 'shipped', 396, '2021-03-05T04:12:51.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('9051547d-ce1a-41c6-a81d-cdb2c114968e', 'd8c34b2c-4b52-4b19-a8cc-24d54ddf0d83', 'cancelled', 712, '2021-10-11T15:26:16.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('51c64f2b-7701-4601-91a1-dd777728d295', '30f4d746-2479-4627-b307-9cf71163dd62', 'placed', 801, '2022-12-21T08:18:55.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('5708982b-9028-43d8-9a0b-7b88ddce5343', 'd8c34b2c-4b52-4b19-a8cc-24d54ddf0d83', 'placed', 263, '2026-02-08T15:20:20.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('8a51e325-2dfc-4d6d-aea3-b4811d8a43a1', '4036a8cd-2761-484c-a461-6a7ee67eeb5a', 'cancelled', 224, '2024-12-21T09:14:11.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('dc168a50-6ccd-4c68-8c7e-6dbc7e79d5c1', '30f4d746-2479-4627-b307-9cf71163dd62', 'placed', 941, '2023-01-17T11:13:03.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('aefa9656-0e97-48e7-807b-c9c2d6befede', '888d989a-32eb-437c-8a53-0c8e2e8e5e12', 'cancelled', 422, '2024-11-28T06:49:53.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "orders" ("id", "user_id", "status", "total_cents", "placed_at")
VALUES ('fdec7aee-b1a2-4f8b-8763-f1cdd022a113', 'd8c34b2c-4b52-4b19-a8cc-24d54ddf0d83', 'placed', 333, '2025-03-07T01:12:16.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('24080922-c715-46ef-8d3e-a1b587125424', '2a7c2149-b84e-4317-a47b-d70dd75c9ee9', 'bravo-juniper-281', 79, 221)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('921d744d-29c8-48a8-b753-36b57a58808a', '5708982b-9028-43d8-9a0b-7b88ddce5343', 'vertex-river-618', 169, 442)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('ca212b73-e466-494d-b226-31ea48dc28f5', '8a51e325-2dfc-4d6d-aea3-b4811d8a43a1', 'quartz tundra', 732, 46)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('2b3beacf-e210-4797-9b73-832ca2387e42', '5708982b-9028-43d8-9a0b-7b88ddce5343', 'Lisbon, Riverside Lane 214', 304, 1)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('83b340be-661e-486e-8e27-5b74518587e8', 'fdec7aee-b1a2-4f8b-8763-f1cdd022a113', 'pebble-alpha-223', 60, 556)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('e14d7578-599c-44a1-bbac-9e5aee787291', '51c64f2b-7701-4601-91a1-dd777728d295', 'omar.holt@example.org', 781, 555)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('ef051327-227b-4cfe-a896-16ea300734c1', '2a7c2149-b84e-4317-a47b-d70dd75c9ee9', 'onyx fable', 589, 996)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('a9dbe452-f4a5-4a96-8d2d-aadf2c408a8e', '51c64f2b-7701-4601-91a1-dd777728d295', 'meadow-pebble-63', 851, 792)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('e7bfe5bb-1159-4ca7-bb77-9818b0fa3bad', '3a357283-6d2c-4d5d-90d3-e605d663e7dd', 'Gus Moreau', 484, 943)
ON CONFLICT DO NOTHING;
INSERT INTO "order_items" ("id", "order_id", "sku", "qty", "unit_price_cents")
VALUES ('100e4842-14dd-4bd6-979c-73a6c2c40cf9', '2a7c2149-b84e-4317-a47b-d70dd75c9ee9', 'harbor-meadow-336', 514, 171)
ON CONFLICT DO NOTHING;
COMMIT;
Frequently asked questions
- Does SchemaSeed need a database connection to generate the seed?
- No. It reads the schema file locally and wrote 3 tables for Orders API with no network access, and --seed 42 reproduces the same bytes.
- Which OpenAPI versions are supported?
- OpenAPI 3.0 and 3.1 with local $ref. On the Orders API fixture that covers 19 constraints across 3 tables; remote $ref and oneOf come back as warnings.
- What does it do with the CHECK, enum, range and length declarations in my schema?
- It reads them and uses them to generate the negative cases that break each one. For Orders API the decisive one is "total_cents" >= 0 on orders.total_cents, and the value that breaks it ships as a negative case. The DDL carries the same declaration as a CHECK constraint, so the database refuses the value too.
- Is the output valid on a fresh database?
- On the Orders API fixture, yes: the reference engine accepted the 3-table DDL and every row satisfied its 19 constraints.
Run it against your own schema
SchemaSeed is MIT licensed and free: unlimited local generation, no account, no telemetry and no usage limit. It reads your schema file on your own machine, so nothing is uploaded anywhere. If it saves you time, you can support the project.
Get SchemaSeed — free Support the project
Questions: support@schemaseed.drz8848.win