How to Generate Relational Test Data From a SQL Schema
If you already have tables, SQL types, and foreign keys, you can generate useful test data without exporting production rows. This guide shows a practical workflow for creating relational CSV fixtures from schema metadata alone.
Why schema-first test data is useful
Many teams start test data generation with a simple row faker. That works for isolated fields, but it often breaks down once you need several related tables to join correctly. Orders need valid customers. Order items need valid orders and products. Event timestamps should happen after the parent entity exists.
A schema-first approach starts from what you already know: table names, column names, SQL types, and foreign keys. That is enough to produce a first synthetic dataset for many development and QA tasks, especially when you want CSV files rather than a live database connection.
This is most useful when you need:
- CSV fixtures for local development
- Related tables with consistent foreign keys
- A saved definition you can review and reuse
- A way to avoid sending sample records
It is less useful when your logic lives mostly outside the schema, such as complex business rules buried in application code, stored procedures, or external services.
What to include in the input schema
The minimum useful input is usually:
- Table names
- Column names
- SQL types
- Foreign-key relationships
- Optional short descriptions for ambiguous columns
For example:
{"tables":[{"name":"customers","columns":[{"name":"customer_id","type":"bigint"},{"name":"country","type":"char(2)"},{"name":"created_at","type":"timestamp"}]},{"name":"orders","columns":[{"name":"order_id","type":"bigint"},{"name":"customer_id","type":"bigint","references":"customers.customer_id"},{"name":"order_total_eur","type":"numeric(10,2)"},{"name":"ordered_at","type":"timestamp"}]}]}Good naming matters. A column called ordered_at tells a generator more than ts_1. If your schema uses cryptic internal names, add short descriptions where possible.
For Luna-specific details on accepted schema fields and outputs, see /docs and /methodology.
Step-by-step workflow
1. List the tables you actually need
Do not start with your entire production schema unless you really need it. Pick the smallest slice that supports the test scenario. For example, checkout tests may only require customers, products, orders, and order_items.
2. Define the size that matches your use case
Think in terms of test purpose, not realism for its own sake.
- For UI testing, a few hundred or thousand rows may be enough.
- For pagination, filtering, and export tests, use larger tables.
- For performance smoke tests, create a shape closer to your expected scale.
3. Define foreign keys explicitly
This is the critical step for relational integrity. If a child table references a parent, include that relationship in the schema. Without foreign keys, you are back to independent fake columns that happen to share names.
4. Generate a preview first
A small preview lets you inspect whether the interpretation of your schema makes sense before spending time on a larger run. Check:
- Do joins work?
- Do date fields look plausible relative to each other?
- Do category-like fields line up with titles or statuses?
If you want a fast starting point, Luna has a free 3-step preview here: https://veri.lunafzai.com/#demo. Treat the preview as a small sample for inspection, not as a full paid-generation run.
5. Review the generation rules
A good generation workflow should not stop at raw CSV output. You also want a machine-readable spec that explains how columns were generated, so you can edit distributions, categories, and consistency rules later.
This matters because the first pass is rarely perfect. You may want a different plan mix, a heavier long tail on order totals, or a different country distribution.
6. Save the spec for future refreshes
Once the shape looks right, keep the spec under version control next to your test fixtures or CI definitions. That gives your team a reusable way to refresh datasets without copying old CSV files forever.
What to validate before using the data
Whether you generate data with your own scripts, an open-source library, or a hosted API, validate a few basics:
| Check | Why it matters |
|---|---|
| Foreign-key integrity | Prevents broken joins and missing parent rows |
| Duplicate rows | Avoids unrealistic accidental repetition |
| Time ordering | Keeps child events after parent creation where appropriate |
| Distribution fit | Helps numeric and category columns look less uniform |
If your generator provides a per-run quality report, review it. If not, add simple checks in SQL or Python before shipping the dataset into QA workflows. Luna describes its quality-report approach in /methodology.
Common approaches compared
Manual scripts
Best when your domain rules are very custom and your engineers are comfortable owning data generation code. The tradeoff is maintenance cost.
Libraries such as Faker
Good for field-level fake values. Less convenient when you need multiple tables, consistent relationships, and reusable rules unless you build that logic yourself.
Spreadsheet-style generators and CSV tools
Useful for quick one-table mock data or demos. They can work for multi-table setups too, but relationships and rule management often become more manual.
Schema-to-dataset APIs
A practical option when you want to submit schema metadata and get related CSV tables plus editable rules back. Luna fits this model: you send a JSON schema and receive related CSV tables, an editable generation spec, and a quality report, without sending sample records.
When this approach is not a fit
Be honest about the limits. Schema-first generation may not be enough if:
- You must mirror a very specific production distribution that only exists in historical data
- Your important constraints are mostly undocumented business logic outside the schema
- You need generated data written directly into a complex vendor-specific database setup
- Your tests depend on edge cases that require handcrafted records
In those cases, use synthetic generation for the broad baseline and add a smaller curated set of scenario-specific fixtures.
A simple buying checklist
If you are evaluating tools for this job, ask:
- Can it generate multiple related tables from foreign keys?
- Can I inspect and edit the generation rules?
- Does it return plain CSV that fits our workflow?
- Can we run a preview before buying?
- Does it require sample records, or can we work from schema only?
- Is the pricing clear for our expected usage? For example, Luna lists Starter at $19 one-time for 50k rows, Pro at $49 one-time for 250k rows, and an API subscription at $19/month for 100k rows.
If those are your priorities, a schema-first workflow is usually the shortest path from table definitions to useful test data.
Try it: Luna Synthetic Relational Test Data
FAQ
- Do I need sample rows to generate relational test data?
- Not necessarily. A schema with table names, SQL types, and foreign keys is often enough for a first synthetic dataset. Sample rows may still help for very custom business rules, but they are not always required.
- What is the biggest mistake when generating relational test data?
- Treating each table independently. If foreign keys, time order, and cross-column rules are not handled together, the data may look fine row by row but fail as soon as your tests exercise joins.
- Should I generate all production tables at once?
- Usually no. Start with the smallest schema slice that supports the test case. It is faster to validate, easier to adjust, and often more useful than a large dataset you do not fully need.