Scenario · target: generate seed data with foreign keys

Seed Product catalog API from OpenAPI: generate seed data Guide

Engineer is blocked because generated rows have dangling foreign keys and the constraint load fails.

Direct answer

SchemaSeed (a Node.js CLI) turns an OpenAPI 3.x document into a PostgreSQL seed script for Product catalog API in which every foreign key resolves to a row that was actually inserted.

This holds for OpenAPI 3.0 and 3.1 with local $ref only; remote $ref URLs and oneOf polymorphism are reported as warnings, and the output targets PostgreSQL 13 or newer.

Run npx --yes @wenlu8848/schemaseed generate --input [the fixture that ships with this page] --out ./seed --formats sql --seed 42 and the script contains 3 CREATE TABLE statements followed by the rows in dependency order, inside one BEGIN/COMMIT pair.

Data as of 2026-11-18 · source: 30 Stack Overflow questions, 4 Hacker News stories, ripple-keypairs at 2,486,628 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

What the generated script actually contains — Product catalog

Running the generator over the Product catalog API fixture produces 3 tables and 15 columns in one transaction. The parent table is written first, the child rows reuse the parent key that was just inserted, and the whole file is wrapped in a single BEGIN/COMMIT pair, so a failure halfway through rolls the database back instead of leaving orphaned rows behind. The constraint count that the generator honours is 22, which is the number of primary key, foreign key, NOT NULL and CHECK clauses it read out of the schema rather than invented. The enum, range and length declarations in the same schema are read out as well, and the value that breaks each of them ships as a negative case.

The failure you are trying to avoid

Hand-written INSERT statements for Product catalog API fail at exactly one point: the first child row whose parent does not exist yet. The engine reports it as foreign_key_violation with SQLSTATE 23503, and the message text on the reference engine was "FOREIGN KEY constraint failed". The generator removes that failure by ordering the tables topologically before it writes a single row, so variants is never written before the row it points at.

Boundary cases that come with the seed

The seed is paired with a negative fixture set. For Product catalog API, the range probe on variants.price_cents uses the value -1, which the constraint "price_cents" >= 0 rejects. The engine produced "CHECK constraint failed: variants_price_cents_check" for that insert. A test suite that only checks the happy path never notices that the API accepts a value it should refuse.

What the output looks like in review

The output is plain SQL, not a binary blob, so it diffs cleanly in code review. Every INSERT in the Product catalog API fixture names its columns explicitly rather than relying on column order, which means the file survives someone adding a column to the schema. The seed file creates the tables products, variants, inventory_items, and each of those tables appears exactly once per seed file.

When a different tool fits better

If the Product catalog API fixture has a single table with no foreign key, a per-schema faker is enough and this page is not the fastest route. The generator earns its place when a schema has more than one table that reference each other, when the output has to be SQL rather than JSON, or when the test suite needs the invalid instances as well as the valid ones.

Real constraint failures for the Product catalog API fixtureReproduced by executing the fixture against a local SQL engine (Node.js v24.22.0, engine 3.53.4) on 2026-11-18T11:53:54.233Z. The message column is verbatim engine output; the SQLSTATE column is the PostgreSQL error class for the same constraint class.
Failure classColumnSQLSTATEError nameEngine message (verbatim)
Missing required fieldproducts.slug23502not_null_violationNOT NULL constraint failed: products.slug
Out-of-contract valuevariants.price_cents23514check_violationCHECK constraint failed: variants_price_cents_check
Dangling foreign keyvariants.product_id23503foreign_key_violationFOREIGN KEY constraint failed
Insert order for Product catalog API, derived from its foreign keysThe generator sorts the tables by this order before writing a single row.
TableReferenced byWritten
productsvariantsafter variants
variantsinventory_itemsafter inventory_items
inventory_items—first, no dependencies
seed.sql

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

Download catalog.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" UUID,
  "slug" TEXT NOT NULL,
  "title" TEXT NOT NULL,
  "status" TEXT NOT NULL,
  "created_at" TIMESTAMPTZ NOT NULL,
  CONSTRAINT "products_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "products_status_check" CHECK ("status" IN ('draft', 'active', 'archived'))
);

CREATE TABLE IF NOT EXISTS "variants" (
  "id" UUID,
  "product_id" UUID NOT NULL,
  "sku" TEXT NOT NULL,
  "price_cents" INTEGER NOT NULL,
  "stock" INTEGER NOT NULL,
  CONSTRAINT "variants_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "variants_price_cents_check" CHECK ("price_cents" >= 0),
  CONSTRAINT "variants_stock_check" CHECK ("stock" >= 0)
);

CREATE TABLE IF NOT EXISTS "inventory_items" (
  "id" UUID,
  "variant_id" UUID NOT NULL,
  "warehouse" TEXT NOT NULL,
  "on_hand" INTEGER NOT NULL,
  "reserved" INTEGER NOT NULL,
  CONSTRAINT "inventory_items_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "inventory_items_on_hand_check" CHECK ("on_hand" >= 0),
  CONSTRAINT "inventory_items_reserved_check" CHECK ("reserved" >= 0)
);

DO $schema_seed$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'variants_product_id_fkey') THEN
    ALTER TABLE "variants" ADD CONSTRAINT "variants_product_id_fkey" FOREIGN KEY ("product_id") REFERENCES "products" ("id");
  END IF;
END
$schema_seed$;

DO $schema_seed$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'inventory_items_variant_id_fkey') THEN
    ALTER TABLE "inventory_items" ADD CONSTRAINT "inventory_items_variant_id_fkey" FOREIGN KEY ("variant_id") REFERENCES "variants" ("id");
  END IF;
END
$schema_seed$;


INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('4a8cd909-e517-4c46-8f21-5247730343fa', 'Berlin, Harbor Road 25', 'Nairobi, Harbor Road 63', 'draft', '2020-04-07T03:24:59.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('84fe808a-6825-4eab-8c22-d0fb2268b923', 'alpha cedar', 'Kai Adler', 'active', '2024-05-04T23:57:31.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('9836d545-8ce6-410c-b2a7-3dceac0ba86a', 'vertex lumen', 'Sami Fontaine', 'active', '2021-05-03T20:23:56.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('13146d96-6b18-4679-b553-9c02bc5969f9', 'Omar Adler', 'zephyr grove', 'archived', '2022-01-23T18:20:21.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('592d38b5-1439-4777-913f-00dbba4291ed', 'Nairobi, Riverside Lane 187', 'nia.novak@test.dev', 'archived', '2023-09-16T09:22:23.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('8a94ba17-3d87-4610-a31c-3a2620da1ef1', 'zephyr grove', 'onyx meadow', 'active', '2020-03-04T23:17:56.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('9bbb6cec-d5f3-442b-9ad7-6af5fa1ea4f7', 'Chen Adler', 'Toronto, Maple Street 221', 'archived', '2024-01-01T02:45:54.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('4e12115f-9dbd-46a4-b87a-9cfa9f6dd656', 'tundra-lumen-699', 'umbra lumen', 'active', '2020-02-23T05:15:02.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('ece850f2-ac3d-4ab0-ba16-ec727bddb458', 'lena.adler@example.org', 'Osaka, Elm Avenue 135', 'archived', '2022-02-14T09:58:55.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "products" ("id", "slug", "title", "status", "created_at")
  VALUES ('52339ba2-69d8-4af8-a2dd-23a7b67ae507', 'Toronto, Maple Street 12', 'dara.bianchi@test.dev', 'draft', '2022-04-12T21:49:49.000Z')
  ON CONFLICT DO NOTHING;

INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('883566d6-4f5f-4a32-9998-89974c17314b', '9836d545-8ce6-410c-b2a7-3dceac0ba86a', 'Hana Lund', 931, 652)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('8f07be40-7c8f-40b8-b7b8-3d898e716186', '52339ba2-69d8-4af8-a2dd-23a7b67ae507', 'Eli Costa', 943, 251)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('70dea1ca-c390-4f8b-99fd-400d24ff71d1', '8a94ba17-3d87-4610-a31c-3a2620da1ef1', 'Osaka, Maple Street 216', 802, 621)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('f209d183-b38d-449c-bbd8-1259d3f3156b', '8a94ba17-3d87-4610-a31c-3a2620da1ef1', 'Nairobi, Oak Drive 219', 105, 932)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('4ea6dfb2-4bd8-49cd-aace-b51fab8612d5', '4a8cd909-e517-4c46-8f21-5247730343fa', 'lumen zephyr', 437, 577)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('c68ca8ea-2cf8-428f-85d7-375f213cadbe', '592d38b5-1439-4777-913f-00dbba4291ed', 'kai.ibarra@mail.example', 728, 940)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('9356884a-90b1-4666-b169-192f3e758c41', '84fe808a-6825-4eab-8c22-d0fb2268b923', 'quartz-umbra-707', 590, 623)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('9967b6e9-c297-47d8-a69e-a73c84c6c505', '52339ba2-69d8-4af8-a2dd-23a7b67ae507', 'Osaka, Oak Drive 5', 686, 651)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('ddcbf503-cce4-465a-a7bd-674739e3883a', 'ece850f2-ac3d-4ab0-ba16-ec727bddb458', 'onyx-grove-152', 581, 785)
  ON CONFLICT DO NOTHING;
INSERT INTO "variants" ("id", "product_id", "sku", "price_cents", "stock")
  VALUES ('95948426-23c8-4ad0-b23e-ad444ceac8e0', '592d38b5-1439-4777-913f-00dbba4291ed', 'indigo meadow', 39, 180)
  ON CONFLICT DO NOTHING;

INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('fae2306c-09f7-47d7-90a8-60188d051fe9', '883566d6-4f5f-4a32-9998-89974c17314b', 'tundra cedar', 445, 63)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('af77eb05-7d75-4154-957e-d6d0d45b6795', 'ddcbf503-cce4-465a-a7bd-674739e3883a', 'Gus Adler', 845, 321)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('3ad201b6-3adf-41c1-a89a-6bca307bca4e', '883566d6-4f5f-4a32-9998-89974c17314b', 'willow-willow-965', 114, 167)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('c1c55a2a-854f-4189-948b-98123bf4da47', 'f209d183-b38d-449c-bbd8-1259d3f3156b', 'Nia Engel', 494, 845)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('717202fb-119f-4eff-b36f-148307010f5e', '95948426-23c8-4ad0-b23e-ad444ceac8e0', 'Toronto, Harbor Road 84', 101, 146)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('a0616982-4f55-4e35-aac4-46e620a79b9c', '8f07be40-7c8f-40b8-b7b8-3d898e716186', 'fable-onyx-291', 318, 525)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('8218641e-74ff-4e61-956b-56ba2c492d25', '883566d6-4f5f-4a32-9998-89974c17314b', 'umbra pebble', 351, 686)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('49ac00b5-e08a-4e89-b8cc-f2bf47ea4890', '95948426-23c8-4ad0-b23e-ad444ceac8e0', 'summit-vertex-308', 97, 495)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('2f1ad798-c457-4337-b37f-abb9a3da611c', '9356884a-90b1-4666-b169-192f3e758c41', 'Pia Moreau', 48, 20)
  ON CONFLICT DO NOTHING;
INSERT INTO "inventory_items" ("id", "variant_id", "warehouse", "on_hand", "reserved")
  VALUES ('a5dd9080-fa22-4aca-a084-dac8bcc7c7cd', '9356884a-90b1-4666-b169-192f3e758c41', 'bravo bravo', 569, 178)
  ON CONFLICT DO NOTHING;

COMMIT;

Frequently asked questions

Does SchemaSeed need a database connection to generate the seed?
No. It reads the schema file locally and wrote 3 tables for Product catalog API with no network access, and --seed 42 reproduces the same bytes.
Which OpenAPI versions are supported?
OpenAPI 3.0 and 3.1 with local $ref. On the Product catalog API fixture that covers 22 constraints across 3 tables; remote $ref and oneOf come back as warnings.
What does it do with the CHECK, enum, range and length declarations in my schema?
It reads them and uses them to generate the negative cases that break each one. For Product catalog API the decisive one is "price_cents" >= 0 on variants.price_cents, and the value that breaks it ships as a negative case. The DDL carries the same declaration as a CHECK constraint, so the database refuses the value too.
Is the output valid on a fresh database?
On the Product catalog API fixture, yes: the reference engine accepted the 3-table DDL and every row satisfied its 22 constraints.

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