{
  "schema_version": 1,
  "diagram_type": "erd",
  "meta": {
    "title": "Order management schema",
    "locale": "en",
    "output": "examples/erd-orders-rendered.html",
    "quality_profile": "showcase",
    "views": [
      {
        "id": "identity",
        "label": "Reference rows",
        "focus": [
          "customer",
          "product"
        ],
        "note": "The rows everything else points at, with their unique keys."
      },
      {
        "id": "checkout",
        "label": "Checkout core",
        "focus": [
          "order",
          "order_item",
          "customer"
        ],
        "note": "order is the hub: order_item and payment both point back at it."
      },
      {
        "id": "followups",
        "label": "Payment and stock",
        "focus": [
          "payment",
          "order",
          "inventory",
          "product"
        ],
        "note": "One optional dashed link and one mandatory stock link."
      }
    ]
  },
  "layout": {
    "mode": "grid",
    "gapY": 38,
    "gapX": 72
  },
  "entities": [
    {
      "id": "customer",
      "label": "customer",
      "tag": "accounts",
      "row": 0,
      "col": 0,
      "attributes": [
        {
          "name": "id",
          "type": "bigint",
          "key": "pk"
        },
        {
          "name": "email",
          "type": "varchar(255)",
          "key": "uk"
        },
        {
          "name": "display_name",
          "type": "varchar(120)"
        },
        {
          "name": "created_at",
          "type": "timestamptz"
        }
      ]
    },
    {
      "id": "order",
      "label": "order",
      "tag": "checkout",
      "row": 0,
      "col": 1,
      "width": 260,
      "attributes": [
        {
          "name": "id",
          "type": "bigint",
          "key": "pk"
        },
        {
          "name": "customer_id",
          "type": "bigint",
          "key": "fk",
          "references": "customer.id"
        },
        {
          "name": "status",
          "type": "varchar(32)"
        },
        {
          "name": "total_amount",
          "type": "numeric(12,2)"
        },
        {
          "name": "currency",
          "type": "char(3)"
        },
        {
          "name": "placed_at",
          "type": "timestamptz"
        },
        {
          "name": "updated_at",
          "type": "timestamptz"
        }
      ]
    },
    {
      "id": "order_item",
      "label": "order_item",
      "tag": "line items",
      "row": 1,
      "col": 2,
      "width": 254,
      "attributes": [
        {
          "name": "id",
          "type": "bigint",
          "key": "pk"
        },
        {
          "name": "order_id",
          "type": "bigint",
          "key": "fk",
          "references": "order.id"
        },
        {
          "name": "product_id",
          "type": "bigint",
          "key": "fk",
          "references": "product.id"
        },
        {
          "name": "quantity",
          "type": "int"
        },
        {
          "name": "unit_price",
          "type": "numeric(12,2)"
        },
        {
          "name": "discount_amount",
          "type": "numeric(12,2)"
        }
      ]
    },
    {
      "id": "product",
      "label": "product",
      "tag": "catalog",
      "row": 1,
      "col": 1,
      "attributes": [
        {
          "name": "id",
          "type": "bigint",
          "key": "pk"
        },
        {
          "name": "sku",
          "type": "varchar(64)",
          "key": "uk"
        },
        {
          "name": "name",
          "type": "varchar(160)"
        },
        {
          "name": "price",
          "type": "numeric(12,2)"
        },
        {
          "name": "cost_amount",
          "type": "numeric(12,2)"
        },
        {
          "name": "active",
          "type": "boolean"
        },
        {
          "name": "created_at",
          "type": "timestamptz"
        }
      ]
    },
    {
      "id": "inventory",
      "label": "inventory",
      "tag": "catalog",
      "row": 1,
      "col": 0,
      "attributes": [
        {
          "name": "id",
          "type": "bigint",
          "key": "pk"
        },
        {
          "name": "product_id",
          "type": "bigint",
          "key": "fk",
          "references": "product.id"
        },
        {
          "name": "warehouse_code",
          "type": "varchar(16)"
        },
        {
          "name": "quantity",
          "type": "int"
        },
        {
          "name": "reserved_quantity",
          "type": "int"
        },
        {
          "name": "updated_at",
          "type": "timestamptz"
        }
      ],
      "width": 236
    },
    {
      "id": "payment",
      "label": "payment",
      "tag": "checkout",
      "row": 0,
      "col": 2,
      "attributes": [
        {
          "name": "id",
          "type": "bigint",
          "key": "pk"
        },
        {
          "name": "order_id",
          "type": "bigint",
          "key": "fk",
          "references": "order.id"
        },
        {
          "name": "method",
          "type": "varchar(32)"
        },
        {
          "name": "amount",
          "type": "numeric(12,2)"
        },
        {
          "name": "paid_at",
          "type": "timestamptz"
        },
        {
          "name": "external_ref",
          "type": "varchar(64)"
        }
      ]
    }
  ],
  "relationships": [
    {
      "id": "order_customer",
      "from": "order",
      "to": "customer",
      "label": "placed by",
      "fromCardinality": "many",
      "toCardinality": "one"
    },
    {
      "id": "payment_order",
      "from": "payment",
      "to": "order",
      "fromCardinality": "many",
      "fromOptional": true,
      "toCardinality": "one",
      "identifying": false
    },
    {
      "id": "order_item_order",
      "from": "order_item",
      "to": "order",
      "fromCardinality": "many",
      "toCardinality": "one"
    },
    {
      "id": "order_item_product",
      "from": "order_item",
      "to": "product",
      "fromCardinality": "many",
      "toCardinality": "one"
    },
    {
      "id": "inventory_product",
      "from": "inventory",
      "to": "product",
      "fromCardinality": "many",
      "toCardinality": "one"
    }
  ],
  "cards": [
    {
      "dot": "cyan",
      "title": "Keys",
      "items": [
        "Every child carries the parent id as a bigint foreign key.",
        "order is the hub: order_item and payment each hold an order_id.",
        "email and sku are unique, so they carry a secondary key marker."
      ]
    },
    {
      "dot": "amber",
      "title": "Optionality",
      "items": [
        "An order may exist before any payment attempt, so the payment side is zero or many.",
        "The payment relationship is non-identifying: it is drawn dashed.",
        "customer, product, and order_item links are all mandatory."
      ]
    },
    {
      "dot": "emerald",
      "title": "How the bands read",
      "items": [
        "The checkout band is the order and the payment that settles it.",
        "The catalog band is what orders point at: one product, one stock record.",
        "accounts and line items stand alone; a single-table domain keeps its tag in the table header."
      ]
    }
  ]
}
