Skip to main content

Insertable

Derive Macro Insertable 

Source
#[derive(Insertable)]
{
    // Attributes available to this derive:
    #[table_name]
    #[auto_increment]
    #[skip_insert]
    #[timestamps]
    #[slug_from]
}
Expand description

Derive macro that generates an insert() method for a struct.

§Attributes

  • #[table_name("table")] — required, specifies the DB table
  • #[auto_increment] — on a field: exclude from INSERT (auto-generated by DB)
  • #[skip_insert] — on a field: exclude from INSERT (e.g. created with DEFAULT)
  • #[timestamps] — on the struct: auto-adds created_at column with Utc::now()
  • #[slug_from("field")] — on a field: auto-generates slug from another field if empty

§Example

#[derive(Insertable)]
#[table_name("posts")]
#[timestamps]
pub struct NewPost {
    pub title: String,
    #[slug_from("title")]
    pub slug: String,
}

// Generated:
// dto.insert(pool).await?;
// → INSERT INTO posts (title, slug, created_at) VALUES (?, ?, ?)
// slug auto-generated from title if empty