[−][src]Derive Macro sqlxinsert::PqInsert
#[derive(PqInsert)]
Create method for inserting struts into Postgres database
ⓘThis example is not tested
#[derive(Default, Debug, std::cmp::PartialEq, sqlx::FromRow)] struct Car { pub id: i32, pub name: String, } #[derive(Default, Debug, sqlx::FromRow, sqlxinsert::PqInsert)] struct CreateCar { pub name: String, pub color: Option<String>, } impl CreateCar { pub fn new<T: Into<String>>(name: T) -> Self { CreateCar { name: name.into(), color: None, } } } let url = "postgres://user:pass@localhost:5432/test_db"; let pool = sqlx::SqlitePool::builder().build(&url).await.unwrap(); let car_skoda = CreateCar::new("Skoda"); let res: Car = car_skoda.insert::<Car>(&pool, "cars").await?;