Tables and keys

Write tables and their columns in schema code, with types, settings, keys, indexes, checks and schemas.

A Table block lists the table's columns, one per line. This page covers what a column can say, and the keys, indexes and checks a table can have.

Tables and columns

Table order_items {
  id integer [pk, increment]
  order_id integer [not null, ref: > orders.id]
  sku varchar(64) [not null]
  quantity integer [not null, default: 1]
  unit_price decimal(10,2) [not null]
  gift_message text [note: 'Printed on the packing slip']
  Note: 'One row per product in an order'
}

Write a column's type as you would for the database. Examples: integer, bigint, varchar(255), text, boolean, timestamp, date, decimal(10,2), uuid, jsonb, or the name of an enum.

The app keeps the type as written. It only checks that a foreign key's type matches the key it points at.

Settings go in square brackets after the type, separated by commas:

SettingWhat it means
pkPart of the primary key. A key column always needs a value.
incrementThe database numbers new rows on its own.
uniqueNo two rows can have the same value.
not nullThe column always needs a value. Without it, a column can be empty.
nullThe column can be empty, which is already the default.
default: <value>The value a new row gets when none is given.
check: <expression>A rule every value must pass, in backticks: check: `price >= 0` .
note: '<text>'What the column holds, when its name doesn't say.
ref: > <table>.<column>The column points at another table's key. See Relationships.

A default is a number, true or false, null, text in single quotes, or an expression in backticks. For example: default: 0, default: true, default: 'draft', default: `now()`.

Note: on a line of its own inside the table says what one row is.

A table needs at least one column. Each column name can be used once in a table.

Keys and indexes

A single-column key is the pk setting. For anything else, add an indexes block inside the table, with one index per line:

Table memberships {
  user_id integer [not null, ref: > users.id]
  team_id integer [not null, ref: > teams.id]
  role varchar(20) [not null]
  joined_at timestamp [not null]
  indexes {
    (user_id, team_id) [pk]
    joined_at
    role [name: 'ix_memberships_role', type: btree]
  }
}
  • (a, b) [pk] makes the columns together the primary key. Use it for a junction table.
  • email [unique] is a unique index, and (a, b) [unique] makes the pair unique.
  • A line with a column, or columns in brackets, and nothing else is a plain index. Use one for columns that are searched or joined often.
  • name: sets the index's name in SQL, and type: its kind, such as btree or hash. note: adds a note.
  • An index can be on an expression in backticks: `lower(email)` [unique].

Checks

A check is a rule every row must pass. For a rule on one column, use the column's check: setting. For a rule across columns, add a checks block inside the table:

Table bookings {
  id integer [pk, increment]
  start_date date [not null]
  end_date date [not null]
  checks {
    `end_date > start_date` [name: 'dates']
  }
}

Each check is an expression in backticks, with an optional name in square brackets.

Schemas and aliases

Table billing.invoices as inv [headercolor: #3498DB] {
  id integer [pk, increment]
}
  • billing.invoices puts the table in the billing schema. Without a schema, a table is in the default one. The SQL for PostgreSQL and SQL Server creates the schema for you.
  • as inv gives the table a short alias that relationships can use, such as ref: > inv.id.
  • headercolor colours the table. See Colours.

Partials and sample rows

A TablePartial holds columns that several tables share, written once. A table takes them in with ~ and the partial's name:

TablePartial timestamps {
  created_at timestamp [not null]
}

Table posts {
  id integer [pk]
  ~timestamps
}

The table shows the partial's columns, but you change them in the partial. On the canvas and in Properties, they're read only.

A Records block holds sample rows for a table. The app keeps them with the table but doesn't draw them. The SQL export inserts them.

Records posts(id, title) {
  1, 'Hello'
}

On this page