Scenario · target: seed database from json schema
Seed Billing and invoices API from OpenAPI: seed database from Guide
User has a JSON Schema file (no OpenAPI) and still wants relational seed data.
Direct answer
SchemaSeed (a Node.js CLI) turns an OpenAPI 3.x document into a PostgreSQL seed script for Billing and invoices 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: 24 Stack Overflow questions, 14 Hacker News stories, json-schema-traverse at 1,803,675,398 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 — Billing and
Running the generator over the Billing and invoices API fixture produces 3 tables and 14 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 20, 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 Billing and invoices 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 invoices 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 Billing and invoices API, the range probe on customers.currency uses the value US, which the constraint char_length("currency") = 3 rejects. The engine produced "CHECK constraint failed: length(currency) = 3" 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 Billing and invoices 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 customers, invoices, invoice_lines, and each of those tables appears exactly once per seed file.
When a different tool fits better
If the Billing and invoices 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 | customers.email | 23502 | not_null_violation | NOT NULL constraint failed: customers.email |
| Out-of-contract value | customers.currency | 23514 | check_violation | CHECK constraint failed: length(currency) = 3 |
| Dangling foreign key | invoices.customer_id | 23503 | foreign_key_violation | FOREIGN KEY constraint failed |
| Table | Referenced by | Written |
|---|---|---|
| customers | invoices | after invoices |
| invoices | invoice_lines | after invoice_lines |
| invoice_lines | — | first, no dependencies |
npx --yes @wenlu8848/schemaseed generate --input ./billing.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 "customers" (
"id" UUID,
"email" TEXT NOT NULL,
"currency" TEXT NOT NULL,
CONSTRAINT "customers_pkey" PRIMARY KEY ("id"),
CONSTRAINT "customers_currency_check" CHECK (char_length("currency") >= 3 AND char_length("currency") <= 3)
);
CREATE TABLE IF NOT EXISTS "invoices" (
"id" UUID,
"customer_id" UUID NOT NULL,
"number" TEXT NOT NULL,
"status" TEXT NOT NULL,
"issued_at" TIMESTAMPTZ NOT NULL,
"total_cents" INTEGER NOT NULL,
CONSTRAINT "invoices_pkey" PRIMARY KEY ("id"),
CONSTRAINT "invoices_status_check" CHECK ("status" IN ('draft', 'open', 'paid', 'void')),
CONSTRAINT "invoices_total_cents_check" CHECK ("total_cents" >= 0)
);
CREATE TABLE IF NOT EXISTS "invoice_lines" (
"id" UUID,
"invoice_id" UUID NOT NULL,
"description" TEXT NOT NULL,
"amount_cents" INTEGER NOT NULL,
"tax_rate_bp" INTEGER NOT NULL,
CONSTRAINT "invoice_lines_pkey" PRIMARY KEY ("id"),
CONSTRAINT "invoice_lines_tax_rate_bp_check" CHECK ("tax_rate_bp" >= 0 AND "tax_rate_bp" <= 10000)
);
DO $schema_seed$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'invoices_customer_id_fkey') THEN
ALTER TABLE "invoices" ADD CONSTRAINT "invoices_customer_id_fkey" FOREIGN KEY ("customer_id") REFERENCES "customers" ("id");
END IF;
END
$schema_seed$;
DO $schema_seed$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'invoice_lines_invoice_id_fkey') THEN
ALTER TABLE "invoice_lines" ADD CONSTRAINT "invoice_lines_invoice_id_fkey" FOREIGN KEY ("invoice_id") REFERENCES "invoices" ("id");
END IF;
END
$schema_seed$;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('ce294ce8-d9c9-4d3e-b536-3ffc029fbe47', 'dara.adler807@mail.example', 'dar')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('2aab9a7c-d513-4bc4-9545-fd906875c133', 'chen.ibarra273@mail.example', 'har')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('cc5d7736-6775-44da-a410-c6cdcc8ce814', 'quinn.moreau133@example.com', 'fab')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('7cb42ce9-7e2e-4218-b228-858b7f91573a', 'milo.fontaine747@mail.example', 'dar')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('aff6d559-b3ac-4140-8ebe-1a3ba8deeda8', 'ada.adler857@example.net', 'Nai')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('5f98ce59-da3b-4190-8286-4b1d8d2163db', 'kai.novak218@example.org', 'Oma')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('ed6e2b32-1f53-4bff-a74d-dd255d145b96', 'kai.novak347@test.dev', 'qui')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('0a99c185-2b5e-4e97-9de6-d4fd88d99b52', 'rosa.bianchi784@example.org', 'bra')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('4dd69736-a602-46f4-8d04-9880d717408e', 'sami.bianchi408@example.com', 'alp')
ON CONFLICT DO NOTHING;
INSERT INTO "customers" ("id", "email", "currency")
VALUES ('e0307a39-73d2-450c-82f2-ec1cf5490503', 'kai.adler321@example.com', 'Lis')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('af829921-93ef-4d77-9861-76ad59d1f8ab', 'aff6d559-b3ac-4140-8ebe-1a3ba8deeda8', 'fable willow', 'draft', '2022-03-09T05:59:07.000Z', 892)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('19277c29-1ddd-4e89-a92b-fb0402e4f009', '0a99c185-2b5e-4e97-9de6-d4fd88d99b52', 'juniper indigo', 'draft', '2020-05-04T05:41:04.000Z', 679)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('29739afe-47c1-4ebb-9e23-cbfc538b5351', '7cb42ce9-7e2e-4218-b228-858b7f91573a', 'harbor zephyr', 'open', '2022-11-27T14:02:48.000Z', 928)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('c544f964-2b16-4f29-9cce-2a6a320eabc8', 'e0307a39-73d2-450c-82f2-ec1cf5490503', 'fable-vertex-536', 'paid', '2022-11-10T22:24:42.000Z', 665)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('4198a541-8f48-4e9d-91fc-03e1c8a5fe69', 'e0307a39-73d2-450c-82f2-ec1cf5490503', 'river onyx', 'draft', '2020-03-20T13:20:29.000Z', 69)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('fb0f6a40-3fb7-4551-8149-e3ae6b9d323e', '2aab9a7c-d513-4bc4-9545-fd906875c133', 'quinn.ibarra@example.com', 'draft', '2021-03-27T12:30:40.000Z', 475)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('f3be912b-bf0c-4df4-bbd9-11de213b0571', 'ed6e2b32-1f53-4bff-a74d-dd255d145b96', 'nia.okafor@mail.example', 'open', '2020-03-20T21:01:41.000Z', 912)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('44cb198f-3840-4edc-b0e5-5cbeb5daeb5d', 'ed6e2b32-1f53-4bff-a74d-dd255d145b96', 'umbra-delta-374', 'paid', '2022-06-21T20:09:43.000Z', 348)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('e23bc309-8796-4d2b-a394-eee080b87942', 'e0307a39-73d2-450c-82f2-ec1cf5490503', 'river fable', 'draft', '2021-08-14T17:46:23.000Z', 837)
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "customer_id", "number", "status", "issued_at", "total_cents")
VALUES ('434fc8da-e51c-4a0f-96f2-e34fba3d2011', '7cb42ce9-7e2e-4218-b228-858b7f91573a', 'lumen pebble', 'paid', '2020-05-28T20:07:36.000Z', 276)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('e4bf3ad7-d23b-42db-aede-a5d297d9cf10', '44cb198f-3840-4edc-b0e5-5cbeb5daeb5d', 'tara.adler@example.net', 268, 9725)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('40cdb0d3-30a7-4054-be42-7ce5f0cd77d4', 'e23bc309-8796-4d2b-a394-eee080b87942', 'delta willow', 213, 6247)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('b489a7cb-2002-459a-9a88-6d9d803250fe', 'f3be912b-bf0c-4df4-bbd9-11de213b0571', 'sami.okafor@mail.example', 75, 7693)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('1c255a09-4074-40e8-9855-3039059e5c64', 'af829921-93ef-4d77-9861-76ad59d1f8ab', 'lena.fontaine@mail.example', 50, 9933)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('422b6594-26da-4048-8420-b1057099e7f4', 'c544f964-2b16-4f29-9cce-2a6a320eabc8', 'grove kelp', 688, 4284)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('889b65d9-33e1-416a-a172-d9bf1916ef78', '434fc8da-e51c-4a0f-96f2-e34fba3d2011', 'umbra-ember-499', 933, 8141)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('e826e951-fd10-41a6-9a6e-c9ba011fc60c', 'af829921-93ef-4d77-9861-76ad59d1f8ab', 'Auckland, Oak Drive 2', 489, 2198)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('892d6eec-50e7-4763-955a-40536e8060e1', '44cb198f-3840-4edc-b0e5-5cbeb5daeb5d', 'Fay Duarte', 106, 1015)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('78c247f5-a9b7-4185-8acd-816a169582db', '4198a541-8f48-4e9d-91fc-03e1c8a5fe69', 'Lisbon, Riverside Lane 198', 748, 7025)
ON CONFLICT DO NOTHING;
INSERT INTO "invoice_lines" ("id", "invoice_id", "description", "amount_cents", "tax_rate_bp")
VALUES ('e888374d-a671-499c-8d18-866be3235b72', '4198a541-8f48-4e9d-91fc-03e1c8a5fe69', 'river nimbus', 922, 4241)
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 Billing and invoices 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 Billing and invoices API fixture that covers 20 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 Billing and invoices API the decisive one is char_length("currency") = 3 on customers.currency, 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 Billing and invoices API fixture, yes: the reference engine accepted the 3-table DDL and every row satisfied its 20 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