Skip to content

Commit

Permalink
docs: insert, update and save return model (SeaQL/sea-orm#339)
Browse files Browse the repository at this point in the history
  • Loading branch information
billy1624 committed Dec 22, 2021
1 parent 05883d6 commit 9ce897c
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 10 deletions.
4 changes: 2 additions & 2 deletions SeaORM/docs/05-basic-crud/02-insert.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,15 @@ assert_eq!(active_model.name, ActiveValue::unchanged("Cheese Cake".to_owned()));

## Insert One

Insert an active model and get back a fresh `ActiveModel`. Its value is retrieved from database, so any auto-generated fields will be populated.
Insert an active model and get back a fresh `Model`. Its value is retrieved from database, so any auto-generated fields will be populated.

```rust
let pear = fruit::ActiveModel {
name: Set("Pear".to_owned()),
..Default::default() // all other attributes are `Unset`
};

let res: fruit::ActiveModel = pear.insert(db).await?;
let pear: fruit::Model = pear.insert(db).await?;
```

Insert an active model and get back the last insert id. Its type matches the model's primary key type, so it could be a tuple if the model has a composite primary key.
Expand Down
4 changes: 2 additions & 2 deletions SeaORM/docs/05-basic-crud/03-update.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ let mut pear: fruit::ActiveModel = pear.unwrap().into();
pear.name = Set("Sweet pear".to_owned());

// Update corresponding row in database using primary key value
let pear: fruit::ActiveModel = pear.update(db).await?;
let pear: fruit::Model = pear.update(db).await?;
```

## Update Many
Expand All @@ -23,7 +23,7 @@ You can also update multiple rows in the database without finding each `Model` w

```rust
// Bulk set attributes using ActiveModel
let pear: fruit::ActiveModel = Fruit::update_many()
let update_result: UpdateResult = Fruit::update_many()
.set(pear)
.filter(fruit::Column::Id.eq(1))
.exec(db)
Expand Down
5 changes: 3 additions & 2 deletions SeaORM/docs/05-basic-crud/04-save.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ let banana = fruit::ActiveModel {
};

// Insert, because primary key `id` is `Unset`
let mut banana = banana.save(db).await?;
let banana: fruit::Model = banana.save(db).await?;
let mut banana: fruit::ActiveModel = banana.into();

banana.name = Set("Banana Mongo".to_owned());

// Update, because primary key `id` is `Set`
let banana = banana.save(db).await?;
let banana: fruit::Model = banana.save(db).await?;
```
34 changes: 30 additions & 4 deletions SeaORM/docs/07-write-test/02-mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,11 +112,25 @@ mod tests {
async fn test_insert_cake() -> Result<(), DbErr> {
// Create MockDatabase with mock execution result
let db = MockDatabase::new(DatabaseBackend::Postgres)
.append_query_results(vec![
vec![cake::Model {
id: 15,
name: "Apple Pie".to_owned(),
}],
vec![cake::Model {
id: 16,
name: "Apple Pie".to_owned(),
}],
])
.append_exec_results(vec![
MockExecResult {
last_insert_id: 15,
rows_affected: 1,
},
MockExecResult {
last_insert_id: 16,
rows_affected: 1,
},
])
.into_connection();

Expand All @@ -127,18 +141,30 @@ mod tests {
};

// Insert the ActiveModel into MockDatabase
let insert_result = apple.insert(&db).await?;
assert_eq!(
apple.clone().insert(&db).await?,
cake::Model {
id: 15,
name: "Apple Pie".to_owned()
}
);

// Checking last insert id
assert_eq!(insert_result.last_insert_id, 15);
// If you want to check the last insert id
let insert_result = cake::Entity::insert(apple).exec(&db).await?;
assert_eq!(insert_result.last_insert_id, 16);

// Checking transaction log
assert_eq!(
db.into_transaction_log(),
vec![
Transaction::from_sql_and_values(
DatabaseBackend::Postgres,
r#"INSERT INTO "cake" ("name") VALUES ($1)"#,
r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id", "name""#,
vec!["Apple Pie".into()]
),
Transaction::from_sql_and_values(
DatabaseBackend::Postgres,
r#"INSERT INTO "cake" ("name") VALUES ($1) RETURNING "id""#,
vec!["Apple Pie".into()]
),
]
Expand Down

0 comments on commit 9ce897c

Please sign in to comment.