Relationships and foreign keys

Connect Rust API models with SQL foreign keys, indexes and deletion rules.

▶ Watch the demo
On this page

User.group_id → Group.id

  1. Create Group with a name field.
  2. Add User with a. After its fields, select Add a relationship (foreign key) (add a foreign key).
  3. Select Group → groups.id and accept group_id.
  4. Choose options and a deletion rule.
  5. Finish and confirm generation.

Miblo generates an integer field, index and SQL constraint. For an existing User, use m → Add an optional relationship (add an optional relationship).

shell
miblo add Group name:string --yes
miblo add User name:string 'group_id:int?:ref=Group' --yes

Choose a deletion rule

RuleEffect on related rows
RESTRICT (default)Prevents deleting a referenced row.
CASCADEAlso deletes rows that reference it.
SET NULLKeeps related rows and clears the reference; requires an optional field.
shell
miblo add Note body:text 'user_id:int?:ref=User:on_delete=set_null' --yes

Unique references and self-references

:unique allows each non-NULL referenced value in only one row. Self-references are supported:

shell
miblo add Folder name:string 'parent_id:int?:ref=Folder:on_delete=set_null' --yes

CRUD exposes IDs, without automatic joins or nested objects. Custom or composite primary keys and automatic many-to-many tables are not generated.

Inspect the schema

  1. Press d to open the actual database schema.
  2. Select a table to see columns, primary and foreign keys, and deletion rules.
  3. p switches to declared models. F5 refreshes; d returns to routes.

Manually created tables are visible too. Internal SQLite and SQLx tables are hidden. This view shows structure; test routes to read rows.

shell
miblo db schema --project team-api

Search for a feature or command.

↑ ↓ browse Enter to open