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

related-test-data.yaml
# 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_key names 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, and belongs_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: customer in the orders, order in the lines.

What it writes

Save the recipe, then run this in the same folder. This is what the program printed for it.

terminal
$ 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_idfirst_namelast_name
618603BradCrane
230298AudreyRivas
103610GlennSimpson
421182AntonioFuentes
473017WayneZuniga

order.csv

The first 5 of its 23 rows.

order_idstatuscustomer
752199new230298
531387paid230298
20002shipped103610
503116shipped103610
207125paid421182

order_line.csv

The first 5 of its 55 rows.

isbnquantityorder
97869166751981752199
97867742329462752199
97869331106723752199
97867390814111531387
9786703366902120002

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.