Scenario · target: populate postgres with mock data for development

Seed Sessions and tokens API from OpenAPI: populate postgres with Guide

Developer needs a local database that behaves like production for feature work.

Direct answer

SchemaSeed (a Node.js CLI) turns an OpenAPI 3.x document into a PostgreSQL seed script for Sessions and tokens 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: 1 Stack Overflow questions, pg at 372,007,475 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 — Sessions and

Running the generator over the Sessions and tokens API fixture produces 3 tables and 14 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 18, 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 Sessions and tokens 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 api_tokens 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 Sessions and tokens API, the range probe on sessions.revoked uses the value 2, which the constraint "revoked" IN (0,1) rejects. The engine produced "CHECK constraint failed: sessions_revoked_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 Sessions and tokens 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 accounts, api_tokens, sessions, and each of those tables appears exactly once per seed file.

When a different tool fits better

If the Sessions and tokens 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 Sessions and tokens 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 fieldaccounts.email23502not_null_violationNOT NULL constraint failed: accounts.email
Out-of-contract valuesessions.revoked23514check_violationCHECK constraint failed: sessions_revoked_check
Dangling foreign keyapi_tokens.account_id23503foreign_key_violationFOREIGN KEY constraint failed
Insert order for Sessions and tokens API, derived from its foreign keysThe generator sorts the tables by this order before writing a single row.
TableReferenced byWritten
accountsapi_tokens, sessionsafter api_tokens, sessions
api_tokenssessionsafter sessions
sessions—first, no dependencies
seed.sql

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

Download sessions.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,
  "created_at" TIMESTAMPTZ NOT NULL,
  CONSTRAINT "accounts_pkey" PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "api_tokens" (
  "id" UUID,
  "account_id" UUID NOT NULL,
  "token_hash" TEXT NOT NULL,
  "scopes" TEXT NOT NULL,
  "expires_at" TIMESTAMPTZ NOT NULL,
  CONSTRAINT "api_tokens_pkey" PRIMARY KEY ("id")
);

CREATE TABLE IF NOT EXISTS "sessions" (
  "id" UUID,
  "account_id" UUID NOT NULL,
  "token_id" UUID NOT NULL,
  "ip" TEXT NOT NULL,
  "started_at" TIMESTAMPTZ NOT NULL,
  "revoked" INTEGER NOT NULL,
  CONSTRAINT "sessions_pkey" PRIMARY KEY ("id"),
  CONSTRAINT "sessions_revoked_check" CHECK ("revoked" >= 0 AND "revoked" <= 1)
);

DO $schema_seed$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'api_tokens_account_id_fkey') THEN
    ALTER TABLE "api_tokens" ADD CONSTRAINT "api_tokens_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "accounts" ("id");
  END IF;
END
$schema_seed$;

DO $schema_seed$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'sessions_account_id_fkey') THEN
    ALTER TABLE "sessions" ADD CONSTRAINT "sessions_account_id_fkey" FOREIGN KEY ("account_id") REFERENCES "accounts" ("id");
  END IF;
END
$schema_seed$;

DO $schema_seed$
BEGIN
  IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = 'sessions_token_id_fkey') THEN
    ALTER TABLE "sessions" ADD CONSTRAINT "sessions_token_id_fkey" FOREIGN KEY ("token_id") REFERENCES "api_tokens" ("id");
  END IF;
END
$schema_seed$;


INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('fc11264a-4c02-4590-b014-b3833871de6c', 'rosa.okafor625@example.org', '2024-05-25T09:39:15.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('0b4361a6-49bc-4e60-8904-20f1bfad1e66', 'rosa.fontaine143@example.net', '2026-07-25T16:59:40.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', 'dara.fontaine413@mail.example', '2020-11-11T23:12:32.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('69adeebe-42fc-419c-b52d-c3abb11d8ae9', 'hana.holt299@example.org', '2021-10-10T23:14:01.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('6c3ea373-cc60-4209-8d2a-a8e7aff4eda6', 'nia.moreau382@example.net', '2020-04-03T11:13:18.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('8152b63e-8147-4a4e-a577-6c291a65e8a6', 'nia.holt905@example.com', '2022-02-13T03:01:26.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('f8ac59e5-01af-40df-aab9-132fec01971f', 'fay.lund790@mail.example', '2026-09-23T11:24:54.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('6a606073-65c4-450e-a35b-2f01fe61aafa', 'fay.okafor636@mail.example', '2025-10-16T20:57:44.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('33903aa4-b914-474a-82bb-99803a8da8bd', 'fay.kaur685@example.org', '2023-01-01T01:37:29.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "accounts" ("id", "email", "created_at")
  VALUES ('62b2ab62-ff01-4d68-986f-8d98247fe7c4', 'kai.costa647@mail.example', '2020-11-03T07:30:32.000Z')
  ON CONFLICT DO NOTHING;

INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('23c9fa87-d1a2-424c-b7da-211705283e19', '4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', 'Auckland, Maple Street 97', 'pebble tundra', '2026-06-28T03:46:09.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('2c4e733d-9b61-4069-9503-62b4751863e1', '6a606073-65c4-450e-a35b-2f01fe61aafa', 'Berlin, Oak Drive 4', 'grove quartz', '2020-03-28T18:21:55.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('8885a24e-67a3-4004-bfb7-4896f6777d02', '62b2ab62-ff01-4d68-986f-8d98247fe7c4', 'chen.ibarra@mail.example', 'juniper-umbra-137', '2020-08-20T03:33:09.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('dd8f72d0-9602-4312-8d37-2ee0112290cf', '4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', 'alpha-umbra-756', 'alpha meadow', '2026-03-07T15:51:32.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('d0a02cd6-cbe4-4e37-b055-789e6a85f9c7', '6a606073-65c4-450e-a35b-2f01fe61aafa', 'Iris Moreau', 'Chen Okafor', '2025-08-05T00:34:51.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('b3dc3d04-cf39-4eef-821a-7b77a7d15a3a', 'fc11264a-4c02-4590-b014-b3833871de6c', 'bravo pebble', 'summit tundra', '2025-04-02T06:12:24.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('695916ab-4cda-484b-aadb-88133c496f27', '6a606073-65c4-450e-a35b-2f01fe61aafa', 'bravo-tundra-355', 'Gus Duarte', '2022-10-01T23:12:17.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('660890bf-180a-48b1-906e-f939bf555be8', 'fc11264a-4c02-4590-b014-b3833871de6c', 'Lisbon, Maple Street 120', 'Tara Jansen', '2024-03-17T23:23:32.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('f425320d-2712-49b4-8b37-610521e3cefd', '8152b63e-8147-4a4e-a577-6c291a65e8a6', 'lumen grove', 'fable-harbor-252', '2021-05-18T15:05:00.000Z')
  ON CONFLICT DO NOTHING;
INSERT INTO "api_tokens" ("id", "account_id", "token_hash", "scopes", "expires_at")
  VALUES ('ec9f508e-5882-43c0-8765-bc20e6eea9ae', '69adeebe-42fc-419c-b52d-c3abb11d8ae9', 'Osaka, Elm Avenue 171', 'summit river', '2024-04-25T17:27:09.000Z')
  ON CONFLICT DO NOTHING;

INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('7b119c32-3ab4-468f-a279-f9b70d4bc935', '69adeebe-42fc-419c-b52d-c3abb11d8ae9', '23c9fa87-d1a2-424c-b7da-211705283e19', 'delta vertex', '2022-12-22T01:51:59.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('94aa54bc-49e6-4692-9f06-08e8dcbbe525', '69adeebe-42fc-419c-b52d-c3abb11d8ae9', 'd0a02cd6-cbe4-4e37-b055-789e6a85f9c7', 'umbra pebble', '2022-12-23T13:10:25.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('2e6c2dfa-02f1-4d10-8522-c0ff7c886c03', '4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', 'f425320d-2712-49b4-8b37-610521e3cefd', 'zephyr-umbra-907', '2026-06-23T00:27:30.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('df81c328-e1a1-42af-b060-5e0d3fb0914a', '4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', '660890bf-180a-48b1-906e-f939bf555be8', 'zephyr-nimbus-387', '2020-01-15T09:26:02.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('38e23787-9c85-4b72-afe0-fd9ba9edf37d', '8152b63e-8147-4a4e-a577-6c291a65e8a6', 'ec9f508e-5882-43c0-8765-bc20e6eea9ae', 'Pia Kaur', '2025-04-25T04:16:28.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('ea770d9a-e43b-4dd3-9a66-c47a94ce1bd1', '4f7a8467-2a3e-4731-ac45-8ab8bcaa18c4', '8885a24e-67a3-4004-bfb7-4896f6777d02', 'tundra cedar', '2023-01-24T16:04:29.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('8aea603b-f7b2-4303-9501-34dbe3172f22', 'fc11264a-4c02-4590-b014-b3833871de6c', '2c4e733d-9b61-4069-9503-62b4751863e1', 'Lisbon, Harbor Road 236', '2022-03-22T03:38:04.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('beddb75c-31ab-437d-af3f-6483bf94dab4', 'f8ac59e5-01af-40df-aab9-132fec01971f', 'f425320d-2712-49b4-8b37-610521e3cefd', 'lumen meadow', '2023-02-20T17:44:01.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('e0ae0b9b-c041-439b-a828-ad7e69dbcbe1', '6a606073-65c4-450e-a35b-2f01fe61aafa', 'b3dc3d04-cf39-4eef-821a-7b77a7d15a3a', 'nia.engel@example.org', '2020-03-10T03:12:26.000Z', 0)
  ON CONFLICT DO NOTHING;
INSERT INTO "sessions" ("id", "account_id", "token_id", "ip", "started_at", "revoked")
  VALUES ('1813ab1c-142e-4382-9c4d-8bb698c83e03', '69adeebe-42fc-419c-b52d-c3abb11d8ae9', 'f425320d-2712-49b4-8b37-610521e3cefd', 'Jun Jansen', '2021-08-17T15:41:20.000Z', 0)
  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 Sessions and tokens 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 Sessions and tokens API fixture that covers 18 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 Sessions and tokens API the decisive one is "revoked" IN (0,1) on sessions.revoked, 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 Sessions and tokens API fixture, yes: the reference engine accepted the 3-table DDL and every row satisfied its 18 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