Spec & format · target: openapi oneof to sql seed

OpenAPI to SQL for Polymorphic payment schema: Full Mapping

User has oneOf/anyOf schemas and does not know how to represent them in rows.

Direct answer

Converting the Polymorphic payment schema schema to SQL is a one-to-one mapping for 6 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 Polymorphic payment schema 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: 0 Stack Overflow questions, 5 Hacker News stories, openapi-sampler at 11,674,360 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 — Polymorphic payment

The table below maps each construct in the Polymorphic payment schema fixture to what it becomes in SQL. 6 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 Polymorphic payment schema, the declaration on payments.amount_cents is "amount_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: payments_amount_cents_check".

A worked example

The Polymorphic payment schema fixture is small enough to convert by hand, which is the point. The generator produces 1 tables — payments — 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.

Boundary probes for every constrained column in Polymorphic payment schemaOne violating value per constraint, with the boundary it crosses.
TableColumnDeclared ruleProbe valueWhy it is invalid
paymentskindone of card, bank_transfernot_a_real_membernot a declared member
paymentskindone of card, bank_transferCARDwrong case
paymentsamount_cents> 00not strictly positive
Insert order for Polymorphic payment schema, derived from its foreign keysThe generator sorts the tables by this order before writing a single row.
TableReferenced byWritten
payments—first, no dependencies
Schema construct to SQL construct for Polymorphic payment schemaDerived from the fixture itself, not from a generic table.
Schema constructSQL formExample in this fixture
object schemaCREATE TABLE payments3 NOT NULL columns
required + foreign keyNOT NULL + FOREIGN KEY REFERENCESnone in this fixture
enumCHECK (col IN (...))card, bank_transfer
minimum / rangeCHECK (col >= n)>0
uniquea plain column; the DDL carries no UNIQUE constraintno unique column in this fixture
seed.sql

npx --yes @wenlu8848/schemaseed generate --input ./spec_oneof.openapi.yaml --out ./seed --formats sql --count 10 --seed 42

Download spec_oneof.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 "payments" (
  "id" UUID,
  "kind" TEXT NOT NULL,
  "amount_cents" INTEGER NOT NULL,
  "card_last4" TEXT,
  "iban" TEXT,
  "reference" TEXT NOT NULL,
  CONSTRAINT "payments_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "payments_kind_check" CHECK ("kind" IN ('card', 'bank_transfer')),
  CONSTRAINT "payments_amount_cents_check" CHECK ("amount_cents" >= 1)
);


INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('4fed803a-a090-4b9c-a116-4d8003899956', 'card', 468, NULL, 'lumen harbor', 'cedar-river-332')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('7aef6623-44b4-474b-9097-9c3b407f6323', 'card', 735, 'cedar quartz', 'Toronto, Riverside Lane 241', 'Omar Costa')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('3d036216-280d-4ae0-bc94-8609dab2f05e', 'card', 50, NULL, 'juniper ember', 'Nairobi, Elm Avenue 177')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('6a0443c8-2ab7-4cf6-b58a-e9dbd4f0c3e0', 'bank_transfer', 933, 'meadow-summit-93', 'onyx lumen', 'juniper nimbus')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "iban", "reference")
  VALUES ('ab52d733-dbd6-46e7-8a21-8d62b03ce6ee', 'card', 390, 'gus.fontaine@example.org', 'vertex-kelp-169')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('e8a8adee-7573-40e3-8611-ecf9295c235f', 'bank_transfer', 449, 'Milo Holt', 'indigo-bravo-578', 'Lisbon, Harbor Road 37')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('93e823b0-bb70-415f-982d-b10391f8758f', 'card', 62, 'Eli Bianchi', 'quartz nimbus', 'Gus Bianchi')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('027048aa-6cd3-4db3-b2c5-22ba76698e80', 'bank_transfer', 821, NULL, 'meadow-cedar-55', 'Berlin, Elm Avenue 19')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('d12e9140-86db-4735-a23c-5ccfe485c30f', 'bank_transfer', 426, 'kai.moreau@example.net', 'Omar Duarte', 'grove-quartz-101')
  ON CONFLICT DO NOTHING;
INSERT INTO "payments" ("id", "kind", "amount_cents", "card_last4", "iban", "reference")
  VALUES ('140fd6ed-5f78-4025-8331-58dcf8037718', 'bank_transfer', 104, 'onyx-umbra-945', 'pia.holt@mail.example', 'vertex-ember-603')
  ON CONFLICT DO NOTHING;

-- (no foreign keys)
COMMIT;

Frequently asked questions

Is the mapping lossless?
For Polymorphic payment schema, 6 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 — payments.amount_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 Polymorphic payment schema fixture was accepted by the reference engine.
How are foreign keys ordered?
Topologically, from the reference graph in the schema. For Polymorphic payment schema the order is payments, 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