Spec & format · target: json schema to sql insert
OpenAPI to SQL for Invoice schema for a keyword mapping table: Full Mapping
User wants SQL generated straight from a JSON Schema document.
Direct answer
Converting the Invoice schema for a keyword mapping table schema to SQL is a one-to-one mapping for 10 constraints in the DDL, and the foreign key order is the only part that needs reasoning.
The mapping assumes the PostgreSQL dialect, which is the only dialect the CLI emits; a native enum type is not used because it cannot be altered in a transaction.
The mapping table below lists the constructs of Invoice schema for a keyword mapping table and their SQL form; the enum row shows the one construct that is written as a CHECK constraint rather than as a native enum type.
Data as of 2026-11-18 · source: 30 Stack Overflow questions, 8 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
The mapping — Invoice schema
The table below maps each construct in the Invoice schema for a keyword mapping table fixture to what it becomes in SQL. 10 constraints in the DDL all have a SQL equivalent: a primary key per table, a foreign key per reference, a NOT NULL per required column and a CHECK for every enum, range and length declaration. The rows are ordered so the parent table comes first.
The cases that need a decision
One construct needs a decision rather than a translation. On Invoice schema for a keyword mapping table, the declaration on invoices.total_cents is "total_cents" >= 0. The generator does not create a native enum type, because a native enum has to be created separately and cannot be changed inside a transaction; it writes the declaration as a CHECK constraint on the column instead. The value that breaks the declaration ships as a negative case, and the reference engine rejected it with "CHECK constraint failed: total_cents >= 0".
A worked example
The Invoice schema for a keyword mapping table fixture is small enough to convert by hand, which is the point. The generator produces 1 tables — invoices — and each child table is written after the table it references. Doing it by hand for one fixture takes a few minutes; doing it for every schema change after that is the cost this removes.
Type mapping (PostgreSQL only)
SchemaSeed emits one SQL dialect, and it is PostgreSQL. The fixture on this page is written in it: uuid stays uuid, a timestamp becomes timestamptz, and json becomes jsonb, and every constraint is emitted with PostgreSQL semantics. No other database engine is targeted, so the type mapping below is the whole mapping.
Checking the result
A converted schema is worth checking against the database rather than by eye. The reference engine accepted the DDL for all 64 fixtures in this matrix and rejected the deliberately invalid rows with the expected error class each time, which is the check that catches a mistranslated constraint before it reaches a developer database.
| Table | Column | Declared rule | Probe value | Why it is invalid |
|---|---|---|---|---|
| invoices | status | one of draft, open, paid | not_a_real_member | not a declared member |
| invoices | status | one of draft, open, paid | DRAFT | wrong case |
| invoices | total_cents | >= 0 | -1 | below the lower bound |
| invoices | currency | exactly 3 characters | SE | too short |
| Table | Referenced by | Written |
|---|---|---|
| invoices | — | first, no dependencies |
| Schema construct | SQL form | Example in this fixture |
|---|---|---|
| object schema | CREATE TABLE invoices | 6 NOT NULL columns |
| required + foreign key | NOT NULL + FOREIGN KEY REFERENCES | none in this fixture |
| enum | CHECK (col IN (...)) | draft, open, paid |
| minimum / range | CHECK (col >= n) | >=0 |
| unique | a plain column; the DDL carries no UNIQUE constraint | number is declared unique in the schema |
npx --yes @wenlu8848/schemaseed generate --input ./spec_invoice.openapi.yaml --out ./seed --formats sql --count 10 --seed 42
Download spec_invoice.openapi.yaml
-- 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 "invoices" (
"id" UUID,
"number" TEXT NOT NULL,
"status" TEXT NOT NULL,
"total_cents" INTEGER NOT NULL,
"issued_at" TIMESTAMPTZ NOT NULL,
"currency" TEXT NOT NULL,
"metadata" JSONB NOT NULL,
CONSTRAINT "invoices_pkey" PRIMARY KEY ("id"),
CONSTRAINT "invoices_status_check" CHECK ("status" IN ('draft', 'open', 'paid')),
CONSTRAINT "invoices_total_cents_check" CHECK ("total_cents" >= 0),
CONSTRAINT "invoices_currency_check" CHECK (char_length("currency") >= 3 AND char_length("currency") <= 3)
);
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('af829921-93ef-4d77-9861-76ad59d1f8ab', 'fable willow', 'draft', 892, '2022-03-09T05:59:07.000Z', 'ind', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('19277c29-1ddd-4e89-a92b-fb0402e4f009', 'juniper indigo', 'open', 679, '2020-05-04T05:41:04.000Z', 'ony', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('29739afe-47c1-4ebb-9e23-cbfc538b5351', 'harbor zephyr', 'paid', 928, '2022-11-27T14:02:48.000Z', 'ind', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('c544f964-2b16-4f29-9cce-2a6a320eabc8', 'fable-vertex-536', 'paid', 665, '2022-11-10T22:24:42.000Z', 'bra', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('4198a541-8f48-4e9d-91fc-03e1c8a5fe69', 'river onyx', 'draft', 69, '2020-03-20T13:20:29.000Z', 'Osa', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('fb0f6a40-3fb7-4551-8149-e3ae6b9d323e', 'quinn.ibarra@example.com', 'open', 475, '2021-03-27T12:30:40.000Z', 'nim', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('f3be912b-bf0c-4df4-bbd9-11de213b0571', 'nia.okafor@mail.example', 'paid', 912, '2020-03-20T21:01:41.000Z', 'Gus', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('44cb198f-3840-4edc-b0e5-5cbeb5daeb5d', 'umbra-delta-374', 'draft', 348, '2022-06-21T20:09:43.000Z', 'sum', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('e23bc309-8796-4d2b-a394-eee080b87942', 'river fable', 'open', 837, '2021-08-14T17:46:23.000Z', 'tun', '{}')
ON CONFLICT DO NOTHING;
INSERT INTO "invoices" ("id", "number", "status", "total_cents", "issued_at", "currency", "metadata")
VALUES ('434fc8da-e51c-4a0f-96f2-e34fba3d2011', 'lumen pebble', 'paid', 276, '2020-05-28T20:07:36.000Z', 'Len', '{}')
ON CONFLICT DO NOTHING;
-- (no foreign keys)
COMMIT;
Frequently asked questions
- Is the mapping lossless?
- For Invoice schema for a keyword mapping table, 10 constraints in the DDL all have a SQL equivalent, and the mapping table above lists each one with the construct it came from. The enum, range and length declarations become CHECK constraints on the column.
- Why not use a native enum type?
- Because CREATE TYPE runs outside the transaction and cannot be altered while it is open. The CLI does not create an enum type either: it reads the members out of the schema and writes them as a CHECK constraint on the column, so the database refuses a value that is not a member — invoices.total_cents on this fixture.
- Does SchemaSeed emit SQL for a database other than PostgreSQL?
- No. The SQL export is PostgreSQL only: uuid, timestamptz and jsonb are the PostgreSQL types and every constraint is emitted with PostgreSQL semantics. The Invoice schema for a keyword mapping table fixture was accepted by the reference engine.
- How are foreign keys ordered?
- Topologically, from the reference graph in the schema. For Invoice schema for a keyword mapping table the order is invoices, and the insert-order table lists each parent.
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