Related tables
Customers, their orders and the lines of each order, from one recipe and one run. Every order carries the key of a customer that exists, and every line the key of an order.
Why tables filled one by one fall short
Fill three tables separately and the keys do not meet: an order names a customer that is not there, and the import or the foreign key refuses it before your test even starts. A recipe describes the tables and the relations between them together, and one run fills them so the keys match.
Describe the relations in the recipe
# Customers, their orders and the lines of each order, in three CSV files whose
# keys match.
#
# Each customer has zero to three orders and each order one to four lines, so
# the relations decide how many orders and lines there are. Each order carries
# the customer_id of its customer in a column named customer, and each line the
# order_id of its order in a column named order.
#
# qadg generate related-test-data.yaml
#
# writes customer.csv, order.csv, order_line.csv and run-note.json. The same
# seed gives the same rows on every machine.
version: "0.1"
seed: "related-test-data"
locale: en-US
entities:
customer:
count: 20
primary_key: [customer_id]
fields:
customer_id: { type: integer, unique: true }
first_name: { generator: first_name }
last_name: { generator: last_name }
order:
belongs_to: { customer: 0-3 }
primary_key: [order_id]
fields:
order_id: { type: integer, unique: true }
status: { values: [new, paid, shipped] }
order_line:
belongs_to: { order: 1-4 }
fields:
isbn: { generator: isbn }
quantity: { values: ["1", "2", "3"] }
output:
format: csv
primary_keynames the column that identifies a row. It is what a child row carries.belongs_to: { customer: 0-3 }gives each customer zero to three orders, so some have none, andbelongs_to: { order: 1-4 }gives each order one to four lines.- A child table needs no
count. The relation decides how many rows it has, and another seed spreads them differently. - Each child gets a column named after its parent, holding the parent’s key:
customerin the orders,orderin the lines.
What it writes
Save the recipe, then run this in the same folder. This is what the program printed for it.
$ qadg generate related-test-data.yaml customer: 20 rows, 3 columns order: 23 rows, 2 columns order_line: 55 rows, 2 columns 3 tables, 98 rows written
customer.csv
The first 5 of its 20 rows.
| customer_id | first_name | last_name |
|---|---|---|
| 618603 | Brad | Crane |
| 230298 | Audrey | Rivas |
| 103610 | Glenn | Simpson |
| 421182 | Antonio | Fuentes |
| 473017 | Wayne | Zuniga |
order.csv
The first 5 of its 23 rows.
| order_id | status | customer |
|---|---|---|
| 752199 | new | 230298 |
| 531387 | paid | 230298 |
| 20002 | shipped | 103610 |
| 503116 | shipped | 103610 |
| 207125 | paid | 421182 |
order_line.csv
The first 5 of its 55 rows.
| isbn | quantity | order |
|---|---|---|
| 9786916675198 | 1 | 752199 |
| 9786774232946 | 2 | 752199 |
| 9786933110672 | 3 | 752199 |
| 9786739081411 | 1 | 531387 |
| 9786703366902 | 1 | 20002 |
run-note.json
The seed, the locale and the versions behind the run, and every table it wrote with its rows and columns.
Each value in the customer column of the orders is a customer_id of a customer, and each value in the order column of the lines is an order_id of an order.
A count that fits the relation
A child table can name a count of its own. The relation then decides only how its rows are spread over the parents, and a count the relation cannot reach is refused before anything is written. The run ends with exit code 5, and the message has the arithmetic: how many rows the parent has, the range, and what the count has to be between.
In JSON
With format: json every order stands inside its customer and every line inside its order, in one file, and each child still names its parent in a field of its own. A shop in three tables is a similar shop in JSON.
What this version does not do yet
Every key in the data matches. This version does not yet damage the relations: a child that points at a parent that is not there, or a key that repeats. What it breaks lists every kind it carries out.