Spec & format · target: convert swagger to sql seed data
OpenAPI to SQL for Swagger 2.0 definition for a construct table: Full Mapping
User has a Swagger 2.0 file and needs SQL out of it.
Direct answer
Converting the Swagger 2.0 definition for a construct table schema to SQL is a one-to-one mapping for 4 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 Swagger 2.0 definition for a construct 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: 1 Stack Overflow questions, swagger-converter at 442,896 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 — Swagger 2.0
The table below maps each construct in the Swagger 2.0 definition for a construct table fixture to what it becomes in SQL. 4 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 Swagger 2.0 definition for a construct table, the declaration on products.price is "price" >= 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: products_price_check".
A worked example
The Swagger 2.0 definition for a construct table fixture is small enough to convert by hand, which is the point. The generator produces 1 tables — products — 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 |
|---|---|---|---|---|
| products | price | >= 0 | -1 | below the lower bound |
| Table | Referenced by | Written |
|---|---|---|
| products | — | first, no dependencies |
| Schema construct | SQL form | Example in this fixture |
|---|---|---|
| object schema | CREATE TABLE products | 2 NOT NULL columns |
| required + foreign key | NOT NULL + FOREIGN KEY REFERENCES | none in this fixture |
| enum | CHECK (col IN (...)) | no enum in this fixture |
| minimum / range | CHECK (col >= n) | >=0 |
| unique | a plain column; the DDL carries no UNIQUE constraint | no unique column in this fixture |
npx --yes @wenlu8848/schemaseed generate --input ./spec_legacy.openapi.yaml --out ./seed --formats sql --count 10 --seed 42
Download spec_legacy.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 "products" (
"id" INTEGER,
"title" TEXT NOT NULL,
"price" NUMERIC,
"tags" TEXT NOT NULL,
"created_at" TIMESTAMPTZ,
CONSTRAINT "products_pkey" PRIMARY KEY ("id"),
CONSTRAINT "products_price_check" CHECK ("price" >= 0)
);
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (365, 'Nairobi, Harbor Road 63', 353.7, 'iris.costa@example.com', '2022-11-24T12:59:39.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (683, 'Kai Adler', 813.7, 'rosa.adler@mail.example', '2021-08-28T21:31:19.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (376, 'Sami Fontaine', 474.2, 'juniper summit', NULL)
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "tags", "created_at")
VALUES (445, 'zephyr grove', 'hana.adler@mail.example', NULL)
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (223, 'nia.novak@test.dev', 757.3, 'sami.engel@mail.example', '2024-12-14T22:23:55.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (856, 'onyx meadow', 551.7, 'vertex river', '2021-12-04T17:56:31.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (942, 'Toronto, Maple Street 221', 874, 'Omar Adler', '2026-01-11T21:54:31.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (635, 'umbra lumen', 230.5, 'delta harbor', '2026-07-18T03:02:11.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "price", "tags", "created_at")
VALUES (528, 'Osaka, Elm Avenue 135', 361.4, 'Bao Ibarra', '2021-06-26T22:55:10.000Z')
ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "title", "tags", "created_at")
VALUES (844, 'dara.bianchi@test.dev', 'Pia Okafor', '2020-12-06T01:49:51.000Z')
ON CONFLICT DO NOTHING;
-- (no foreign keys)
COMMIT;
Frequently asked questions
- Is the mapping lossless?
- For Swagger 2.0 definition for a construct table, 4 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 — products.price 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 Swagger 2.0 definition for a construct table fixture was accepted by the reference engine.
- How are foreign keys ordered?
- Topologically, from the reference graph in the schema. For Swagger 2.0 definition for a construct table the order is products, 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