Skip to content
Back to skills

Database And Models

ASecurity

Builds models, relationships, typed queries, migrations, schema changes, and factories in Laravel for Rust. Use when creating or modifying models under src/models/, writing migrations under database/migrations/, querying the database, or defining factories.

  • 8 stars
  • 0 votes
  • 0 copies
  • 0 views
  • Added December 2, 2026
developmentrustgosqlapidatabase

Works with

  • api

Security analysis

A100/100

Scanned December 2, 2026

npx -y skills add danielhe4rt/overthinking-canvas-rs --skill database-and-models --agent claude-code

Installs into .claude/skills of the current project.

Are you the author of Database And Models?

Add the live security badge to your README. It updates with every re-scan.

Security grade badge for Database And Models
[![Security: A — Skills Directory](https://www.skillsdirectory.com/api/skills/danielhe4rt-database-and-models/badge)](https://www.skillsdirectory.com/skills/danielhe4rt-database-and-models)

More formats (shields.io, HTML) on the badges page. Keep it an A: scan every change in CI with Pro.

Download with Pro
SKILL.md
---
name: database-and-models
description: Builds models, relationships, typed queries, migrations, schema changes, and factories in Laravel for Rust. Use when creating or modifying models under src/models/, writing migrations under database/migrations/, querying the database, or defining factories.
---

# Database, Models & Migrations

Consult the installed framework docs (locate via `.agents/skills/writing-laravel-rust-apps/scripts/docs-path.sh`) for full API details: `models.md`, `model-relationships.md`, `migrations.md`, `schema.md`, `queries.md`, `pagination.md`, `factories.md`, and `database.md`.

## Models & Relationships

- Annotate model structs with `#[model]`. The macro automatically derives `Clone`, `Debug`, and `Serialize`—do not derive them again yourself.
- Mark columns that have database defaults with `#[default]` so `create!` lets you omit them and returns the database-assigned values. Wrap nullable columns in `Option<T>`.
- Keep each model, its model-specific enums, its authorization policy, and its factory together in the same `src/models/<model>.rs` file—ordered top-down as model, then policy, then factory—and re-export them from `src/models/mod.rs`.
- Filter and order with typed model columns (`Post::votes.gt(100) & Post::published_at.is_not_null()`, `Post::published_at.desc()`) so column names and types are checked at compile time.
- Write records with `create!` and `update!`, using `#[laravel::input(fills = Post)]` fill structs for request data so untrusted fields can never be mass-assigned.
- Relationships (`#[belongs_to]`, `#[has_many]`, `#[has_one]`, `#[belongs_to_many]`) are never lazy-loaded in Laravel for Rust. Eager load what you need with `.with(...)` (e.g., `.with((Post::user, Post::comments))`), or query them explicitly via `post.comments()`.
- Use `with_count` instead of loading full relationships just to count them, chunk large datasets rather than loading everything into memory, and pair pagination with a deterministic `order_by` (adding a unique tie-breaker like `Post::id.desc()` when order matters).
- Never interpolate untrusted input into raw SQL; bind parameters and allow-list any user-chosen sort columns.

```rust
// src/models/post.rs
use laravel::model::{BelongsTo, HasMany, model};
use laravel::time::Timestamp;

use crate::models::{Comment, User};

#[model]
pub struct Post {
    pub id: i64,
    pub user_id: i64,
    pub title: String,
    #[default]
    pub votes: i64,
    pub published_at: Option<Timestamp>,
    pub created_at: Timestamp,
    pub updated_at: Timestamp,

    #[belongs_to]
    pub user: BelongsTo<User>,
    #[has_many]
    pub comments: HasMany<Comment>,
}
```

## Migrations & Schema

- Generate migrations with `laravel make:migration create_posts_table` (or `cargo run -- make:migration ...`). Migrations are Rust files in `database/migrations/` that compile into the binary via `build.rs`.
- Migrations are forward-only—there is no `down` function or rollback command. Never edit or rename a migration that has already run in a shared or production environment; write a new forward migration instead. During local development before deploy, you can edit an unshipped migration and rebuild with `laravel migrate:fresh`.
- Accept `&mut AnyTransaction<'_>` by default so Laravel runs the migration inside a transaction. Accept `&AnyConnection` only when a statement cannot run in a transaction (such as a concurrent PostgreSQL index build or a batched backfill).
- On MySQL and MariaDB, DDL statements commit implicitly—keep each table change in its own migration file if targeting MySQL.
- Use the built-in macros for framework-managed tables rather than hand-writing their schemas: `laravel::cache::tables!()`, `laravel::feature::table!()`, `laravel::queue::tables!()`, `laravel::session::table!()`, and `laravel::workflow::tables!()`.

```rust
// database/migrations/2026_10_27_143000_create_posts_table.rs
use laravel::db::AnyTransaction;

pub async fn migrate(tx: &mut AnyTransaction<'_>) -> laravel::Result<()> {
    tx.schema()
        .create("posts", |table| {
            table.id();
            table.foreign_id("user_id").constrained().cascade_on_delete();
            table.string("title");
            table.timestamp("published_at").nullable();
            table.timestamps();

            table.index(["user_id", "published_at"]);
        })
        .await
}
```

Attribution

Is this your skill, or is something wrong with this listing? Request removal or report an issue. Author removals are honored within 72 hours.

View all in development →

Comments

Loading comments…

Newsletter

Get the weekly list of safe new skills, and the ones we pulled.