Spec & format · target: json schema nullable to sql

OpenAPI to SQL for Nullable field schema for a nullability table: Full Mapping

User must decide NOT NULL vs NULL for each field from the schema.

Direct answer

Converting the Nullable field schema for a nullability table schema to SQL is a one-to-one mapping for 5 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 Nullable field schema for a nullability 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, zod-to-json-schema at 380,614,481 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 — Nullable field

The table below maps each construct in the Nullable field schema for a nullability table fixture to what it becomes in SQL. 5 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 Nullable field schema for a nullability table, the declaration on accounts.locale is char_length("locale") BETWEEN 2 AND 5. 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: length(locale) BETWEEN 2 AND 5".

A worked example

The Nullable field schema for a nullability table fixture is small enough to convert by hand, which is the point. The generator produces 1 tables — accounts — 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 Nullable field schema for a nullability tableOne violating value per constraint, with the boundary it crosses.
TableColumnDeclared ruleProbe valueWhy it is invalid
accountslocale2 to 5 charactersetoo short
Insert order for Nullable field schema for a nullability table, derived from its foreign keysThe generator sorts the tables by this order before writing a single row.
TableReferenced byWritten
accounts—first, no dependencies
Schema construct to SQL construct for Nullable field schema for a nullability tableDerived from the fixture itself, not from a generic table.
Schema constructSQL formExample in this fixture
object schemaCREATE TABLE accounts3 NOT NULL columns
required + foreign keyNOT NULL + FOREIGN KEY REFERENCESnone in this fixture
enumCHECK (col IN (...))no enum in this fixture
minimum / rangeCHECK (col >= n)2to5
uniquea plain column; the DDL carries no UNIQUE constraintemail is declared unique in the schema
seed.sql

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

Download spec_nullable.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 "accounts" (
  "id" UUID,
  "email" TEXT NOT NULL,
  "display_name" TEXT NOT NULL,
  "nickname" TEXT,
  "phone" TEXT,
  "locale" TEXT NOT NULL,
  CONSTRAINT "accounts_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "accounts_locale_check" CHECK (char_length("locale") >= 2 AND char_length("locale") <= 5)
);


INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "locale")
  VALUES ('fc11264a-4c02-4590-b014-b3833871de6c', 'rosa.okafor625@example.org', 'Chen Moreau', 'cedar pebble', 'Quinn')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "phone", "locale")
  VALUES ('0b4361a6-49bc-4e60-8904-20f1bfad1e66', 'rosa.fontaine143@example.net', 'ember meadow', 'fable onyx', 'ember-pebble-311', 'Nia E')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "phone", "locale")
  VALUES ('4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', 'dara.fontaine413@mail.example', 'bao.grant@example.com', 'Lisbon, Harbor Road 103', 'Pia Okafor', 'Osaka')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "phone", "locale")
  VALUES ('69adeebe-42fc-419c-b52d-c3abb11d8ae9', 'hana.holt299@example.org', 'juniper bravo', 'tara.kaur@example.org', 'kai.kaur@mail.example', 'Osaka')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "phone", "locale")
  VALUES ('6c3ea373-cc60-4209-8d2a-a8e7aff4eda6', 'nia.moreau382@example.net', 'juniper willow', 'quinn.costa@mail.example', 'grove')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "phone", "locale")
  VALUES ('8152b63e-8147-4a4e-a577-6c291a65e8a6', 'nia.holt905@example.com', 'zephyr river', 'umbra bravo', NULL, 'Rosa ')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "phone", "locale")
  VALUES ('f8ac59e5-01af-40df-aab9-132fec01971f', 'fay.lund790@mail.example', 'dara.lund@mail.example', 'Fay Kaur', 'fable')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "phone", "locale")
  VALUES ('6a606073-65c4-450e-a35b-2f01fe61aafa', 'fay.okafor636@mail.example', 'lumen delta', 'Bao Moreau', 'lumen-lumen-991', 'umbra')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "locale")
  VALUES ('33903aa4-b914-474a-82bb-99803a8da8bd', 'fay.kaur685@example.org', 'meadow kelp', 'harbor willow', 'Ada P')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "display_name", "nickname", "phone", "locale")
  VALUES ('62b2ab62-ff01-4d68-986f-8d98247fe7c4', 'kai.costa647@mail.example', 'eli.grant@test.dev', 'tundra grove', 'fay.engel@example.net', 'sami.')
  ON CONFLICT DO NOTHING;

-- (no foreign keys)
COMMIT;

Frequently asked questions

Is the mapping lossless?
For Nullable field schema for a nullability table, 5 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 — accounts.locale 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 Nullable field schema for a nullability table fixture was accepted by the reference engine.
How are foreign keys ordered?
Topologically, from the reference graph in the schema. For Nullable field schema for a nullability table the order is accounts, 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