Rust API example: users, groups and foreign keys
Example tested on with Miblo 0.9.1 · NaSuRall
Connect User and Group in a Rust API: generate a SQL foreign key, preserve existing rows and add POST /members with Miblo CLI.
On this page
Start from a working API
Follow the Axum and SQLite tutorial first, including the POST that creates Alice. Run the commands below inside team-api. This example uses a local SQLite database and Miblo 0.9.1 with templates 0.6.0.
We will link each user to an optional group. Miblo generates the Rust types, SQL migration and CRUD queries. It does not infer permissions or group membership rules.
1. Add Group and the foreign key
Stop the API with Ctrl+C in its terminal, or s if Miblo started it. Then run:
miblo add Group name:string --yes
miblo edit User 'group_id:int?:ref=Group' --yes
miblo db migrate
miblo runint? makes the field optional; ref=Group points to groups.id. Existing users keep their IDs, names and emails; their new group_id starts as NULL. The migration adds the foreign key and its index.
| Table | Primary key | Relationship |
|---|---|---|
| groups | id | One group can be referenced by several users. |
| users | id | group_id → groups.id, optional. |
This is a foreign-key relationship, not an embedded Group object in the JSON response. The default deletion rule is RESTRICT: unlink users before deleting their group. Relationship options →
2. Check that Alice is still there
Keep the API running. In a second terminal:
curl http://127.0.0.1:3000/users/1For Alice created as the first user in the tutorial, the response is:
{"id":1,"name":"Alice","email":"alice@example.com","group_id":null}No existing row was deleted or replaced. See model updates and migrations for changes to your own project.
3. Create a group and assign it
curl -i http://127.0.0.1:3000/groups \
-H 'Content-Type: application/json' \
-d '{"name":"Engineering"}'
curl -i -X PATCH http://127.0.0.1:3000/users/1 \
-H 'Content-Type: application/json' \
-d '{"group_id":1}'The group POST returns 201; the user PATCH returns 200 with group_id: 1. Replace both IDs with the actual returned values if you already have records. A reference to a missing group returns 400.
In the interface, create Group with a, then edit User with m and choose the relation field. Apply the migration with b, restart the API, and inspect the schema with d. Use x to test either route with a form.
4. Add POST /members using the User model
Stop the API again. This command adds an alternative creation endpoint using the existing User SQL code:
miblo route add create_member --resource User --action create --path /members --yes
miblo runIn the second terminal:
curl -i http://127.0.0.1:3000/members \
-H 'Content-Type: application/json' \
-d '{"name":"Bob","email":"bob@example.com","group_id":1}'Expected result: HTTP 201. Bob is inserted into users and appears in GET /users; no members table is created. In the interface, press g, choose User and the create action, then set /members.
--action create connects the route to generated SQL. Associating a model without an action only creates a function to implement. Custom route options →
Where your own code belongs
Add group membership rules in src/resources/user/service.rs, and custom SQL or joins in repository.rs. Authentication, access control and deployment remain part of your application work. Read the generated code →
This walkthrough was executed against SQLite. Miblo also generates MySQL projects, which require a running database server and connection settings.