| 16 | } |
| 17 | |
| 18 | pub async fn insert_and_update(db: &DbConn) -> Result<(), DbErr> { |
| 19 | let pear = fruit::ActiveModel { |
| 20 | name: Set("pear".to_owned()), |
| 21 | ..Default::default() |
| 22 | }; |
| 23 | let res = Fruit::insert(pear).exec(db).await?; |
| 24 | |
| 25 | println!("Inserted: last_insert_id = {}", res.last_insert_id); |
| 26 | |
| 27 | let pear: Option<fruit::Model> = Fruit::find_by_id(res.last_insert_id).one(db).await?; |
| 28 | |
| 29 | println!("Pear: {pear:?}"); |
| 30 | |
| 31 | let mut pear: fruit::ActiveModel = pear.unwrap().into(); |
| 32 | pear.name = Set("Sweet pear".to_owned()); |
| 33 | |
| 34 | let pear: fruit::Model = pear.update(db).await?; |
| 35 | |
| 36 | println!("Updated: {pear:?}"); |
| 37 | |
| 38 | let result = pear.delete(db).await?; |
| 39 | |
| 40 | println!("Deleted: {result:?}"); |
| 41 | |
| 42 | Ok(()) |
| 43 | } |
| 44 | |
| 45 | pub async fn save_active_model(db: &DbConn) -> Result<(), DbErr> { |
| 46 | let banana = fruit::ActiveModel { |