Relationships and foreign keys
Connect Rust API models with SQL foreign keys, indexes and deletion rules.
▶ Watch the demoOn this page
User.group_id → Group.id
- Create
Groupwith anamefield. - Add
Userwith a. After its fields, select Add a relationship (foreign key) (add a foreign key). - Select Group → groups.id and accept
group_id. - Choose options and a deletion rule.
- 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' --yesChoose a deletion rule
| Rule | Effect on related rows |
|---|---|
| RESTRICT (default) | Prevents deleting a referenced row. |
| CASCADE | Also deletes rows that reference it. |
| SET NULL | Keeps 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' --yesUnique 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' --yesCRUD 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
- Press d to open the actual database schema.
- Select a table to see columns, primary and foreign keys, and deletion rules.
- 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