#[derive(Insert)]
{
// Attributes available to this derive:
#[sql]
}
Expand description
fn insert(&self, exec: impl sqlx::SqliteExecutor<’_ >) -> Result<i64, sqlx::Error>
Generate fn for INSERT with sqlx.
If there is a PRIMARY KEY and the rust-type is i64 then the insert depends on the value of the pk.
If pk > 0 then do a full insert including the pk, else do a insert without the pk so sqlite assigns a new one,
which is returned as result.
Without PRIMARY KEY or rust-type != i64 do a full insert and return the last_insert_rowid.
PRIMARY KEY detection works only if constraint is in all caps, lowercase serves as escape hatch.
#[derive(CreateTableSql,Insert)]
struct Cat {
#[sql(constraint = "PRIMARY KEY")]
id: i64,
name: String,
}
#[tokio::main(flavor = "current_thread")]
async fn main() -> Result<(), sqlx::Error> {
use sqlx::{Connection, Executor};
let mut conn = sqlx::SqliteConnection::connect(":memory:").await?;
conn.execute(Cat::CREATE_TABLE_SQL).await?;
let c = Cat {
id: 0,
name: "miau".to_owned(),
};
let id = c.insert(&mut conn).await?;
assert!(id == 1);
Ok(())
}