Spec & format · target: swagger json to insert statements
OpenAPI to SQL for Shipment schema for a worked conversion: Full Mapping
User wants plain INSERT statements out of a Swagger JSON file.
Direct answer
Converting the Shipment schema for a worked conversion schema to SQL is a one-to-one mapping for 11 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 Shipment schema for a worked conversion 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: 2 Stack Overflow questions, swagger-ui-express at 23,348,934 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 — Shipment schema
The table below maps each construct in the Shipment schema for a worked conversion fixture to what it becomes in SQL. 11 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 Shipment schema for a worked conversion, the declaration on shipments.weight_grams is "weight_grams" > 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: shipments_weight_grams_check".
A worked example
The Shipment schema for a worked conversion fixture is small enough to convert by hand, which is the point. The generator produces 2 tables — carriers, shipments — 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 |
|---|---|---|---|---|
| shipments | status | one of label_created, in_transit, delivered | not_a_real_member | not a declared member |
| shipments | status | one of label_created, in_transit, delivered | LABEL_CREATED | wrong case |
| shipments | weight_grams | > 0 | 0 | not strictly positive |
| Table | Referenced by | Written |
|---|---|---|
| carriers | shipments | after shipments |
| shipments | — | first, no dependencies |
| Schema construct | SQL form | Example in this fixture |
|---|---|---|
| object schema | CREATE TABLE carriers | 2 NOT NULL columns |
| object schema | CREATE TABLE shipments | 4 NOT NULL columns |
| required + foreign key | NOT NULL + FOREIGN KEY REFERENCES | carriers referenced by carrier_id |
| enum | CHECK (col IN (...)) | label_created, in_transit, delivered |
| minimum / range | CHECK (col >= n) | >0 |
| unique | a plain column; the DDL carries no UNIQUE constraint | code is declared unique in the schema |
npx --yes @wenlu8848/schemaseed generate --input ./spec_shipment.openapi.yaml --out ./seed --formats sql --count 10 --seed 42
Download spec_shipment.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 "carriers" (
"id" UUID,
"code" TEXT NOT NULL,
"name" TEXT NOT NULL,
CONSTRAINT "carriers_pkey" PRIMARY KEY ("id")
);
CREATE TABLE IF NOT EXISTS "shipments" (
"id" UUID,
"carrier_id" UUID NOT NULL,
"tracking" TEXT NOT NULL,
"status" TEXT NOT NULL,
"weight_grams" INTEGER NOT NULL,
CONSTRAINT "shipments_pkey" PRIMARY KEY ("id"),
CONSTRAINT "shipments_status_check" CHECK ("status" IN ('label_created', 'in_transit', 'delivered')),
CONSTRAINT "shipments_weight_grams_check" CHECK ("weight_grams" >= 1)
);
DO $schema_seed$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'shipments_carrier_id_fkey') THEN
ALTER TABLE "shipments" ADD CONSTRAINT "shipments_carrier_id_fkey" FOREIGN KEY ("carrier_id") REFERENCES "carriers" ("id");
END IF;
END
$schema_seed$;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('8b3d36c5-2c64-444d-ad84-3fc1eedf38c4', 'Ada Adler', 'vertex fable')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('3b7f82af-9402-4fbc-a8b3-ecba5ecd7e13', 'onyx harbor', 'cedar meadow')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('ba3be5e2-4095-4eb0-b907-efce16a2f831', 'zephyr-pebble-786', 'Osaka, Elm Avenue 142')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('e0406931-ab85-4deb-82cb-e149ee87655c', 'zephyr ember', 'pebble vertex')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('2788ccea-6443-4d62-a81b-39f83440070b', 'harbor-pebble-996', 'Nairobi, Elm Avenue 66')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('7d29f36f-c495-4424-a4c8-569c1a98521e', 'Lisbon, Harbor Road 91', 'Osaka, Riverside Lane 79')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('b82ba0bb-f607-4f4b-8de2-03b82abf180f', 'Lisbon, Riverside Lane 227', 'nimbus quartz')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('2df7b30d-0e65-4837-a81b-42ea8813dc38', 'Toronto, Maple Street 23', 'quartz-willow-879')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('1b79d66f-9a5e-4450-9eb5-18ee74b6a94f', 'zephyr zephyr', 'Toronto, Harbor Road 89')
ON CONFLICT DO NOTHING;
INSERT INTO "carriers" ("id", "code", "name")
VALUES ('6d7bcdcb-5b30-48d8-a9b0-a14cb92b2f2d', 'lumen ember', 'kelp willow')
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('9db1f3f7-7a5b-4e2d-9d32-3aa1d4cdf29f', 'b82ba0bb-f607-4f4b-8de2-03b82abf180f', 'juniper-tundra-136', 'in_transit', 944)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('93ef7252-a043-4a22-98b9-3f2ffe2ad0a1', 'b82ba0bb-f607-4f4b-8de2-03b82abf180f', 'Osaka, Oak Drive 167', 'label_created', 521)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('f43df7ac-05f6-45b9-8450-8b5cd72f9ecc', 'b82ba0bb-f607-4f4b-8de2-03b82abf180f', 'river alpha', 'delivered', 405)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('b98ae8d4-e1bf-40f9-b174-3a2fbfa06647', 'b82ba0bb-f607-4f4b-8de2-03b82abf180f', 'tundra-indigo-97', 'in_transit', 647)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('72b2ec2a-021a-4ceb-9f73-a1aeeb70afb7', '2788ccea-6443-4d62-a81b-39f83440070b', 'harbor-juniper-86', 'label_created', 547)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('5ca7b736-0d13-49ac-8160-dfd96654f9e5', '8b3d36c5-2c64-444d-ad84-3fc1eedf38c4', 'Berlin, Oak Drive 22', 'delivered', 438)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('bf6a5232-40f6-4034-b7ac-da995913a381', '7d29f36f-c495-4424-a4c8-569c1a98521e', 'tundra delta', 'label_created', 478)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('2d7376b5-b741-40a5-8bab-cb09220ba2c1', '2788ccea-6443-4d62-a81b-39f83440070b', 'quartz umbra', 'delivered', 491)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('2deb85a0-5233-4d45-808f-c757382a25c1', '2df7b30d-0e65-4837-a81b-42ea8813dc38', 'ember tundra', 'label_created', 283)
ON CONFLICT DO NOTHING;
INSERT INTO "shipments" ("id", "carrier_id", "tracking", "status", "weight_grams")
VALUES ('01f2cb56-c794-4d19-a999-5fdd3a22a060', '2788ccea-6443-4d62-a81b-39f83440070b', 'indigo grove', 'delivered', 276)
ON CONFLICT DO NOTHING;
COMMIT;
Frequently asked questions
- Is the mapping lossless?
- For Shipment schema for a worked conversion, 11 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 — shipments.weight_grams 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 Shipment schema for a worked conversion fixture was accepted by the reference engine.
- How are foreign keys ordered?
- Topologically, from the reference graph in the schema. For Shipment schema for a worked conversion the order is carriers → shipments, 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