Skip to main content

Insert

Derive Macro Insert 

Source
#[derive(Insert)]
{
    // Attributes available to this derive:
    #[sqly]
}
Expand description

Applied to types which are defined to insert rows into a table.

Implements InsertImpl. Might also implement InsertCheck and Insert.


§Attribute Definition (see Attribute Notation and Attribute Documentation)

§Struct Attributes:

#[sqly((table)! (= Path|String)!)] // required
#[sqly((rename_all)? (= String)!)]

#[sqly((dynamic)?)]
#[sqly((optional)?)]
#[sqly((returning)? (= Path? { Ident,+ }? )?)]

#[sqly((crate)? (= Path)!)]
#[sqly((unchecked)? (= query|types)?)]

#[sqly((print)? (= panic|warn|stdout|stderr)?)]
#[sqly((debug)? (= panic|warn|stdout|stderr)?)]

§Field Attributes:

#[sqly((column)? (= String)!)]
#[sqly((rename)? (= String)!)]

#[sqly((insert)* (= String)+)]
#[sqly((optional)? (= bool)?)]
#[sqly((value)? (= Expr)!)]
#[sqly((infer)?)]

#[sqly((skip)?)]


§Example

use sqly::derive::*; // traits
 
#[derive(Table)]
#[sqly(table = "books")]
struct Book {
    // ...
    // these fields are ignored
}
 
#[derive(Insert)]
#[sqly(table = Book)]
struct InsertBook {
    title: String, // all insert fields are values
    #[sqly(skip)]
    info: &'static str, // except when skipped
}
 
async fn insert_book(title: String, db: &sqlx::PgPool) -> Result<()> {
    Book::insert(&InsertBook {
        title: title, // value
        info: "..." // ignored
    }) // returns `sqlx::query::Query`
    .execute(db)
    .await?;
    Ok(())
}