Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

we need to borrow or else Rust compiler complains #24

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions tutorials-book/src/ch01-05-basic-crud-operations.md
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async fn run() -> Result<(), DbErr> {
profit_margin: ActiveValue::Set(0.0),
..Default::default()
};
let res = Bakery::insert(happy_bakery).exec(db).await?;
let res = Bakery::insert(happy_bakery).exec(&db).await?;
}
```

Expand All @@ -57,7 +57,7 @@ let sad_bakery = bakery::ActiveModel {
name: ActiveValue::Set("Sad Bakery".to_owned()),
profit_margin: ActiveValue::NotSet,
};
sad_bakery.update(db).await?;
sad_bakery.update(&db).await?;
```

Let's welcome John, the first employee of _Sad Bakery_!
Expand All @@ -68,7 +68,7 @@ let john = chef::ActiveModel {
bakery_id: ActiveValue::Set(res.last_insert_id), // a foreign key
..Default::default()
};
Chef::insert(john).exec(db).await?;
Chef::insert(john).exec(&db).await?;
```

## Find (single entity)
Expand All @@ -77,17 +77,17 @@ We can find all or some of the bakeries in the database as follows:

```rust, no_run
// Finding all is built-in
let bakeries: Vec<bakery::Model> = Bakery::find().all(db).await?;
let bakeries: Vec<bakery::Model> = Bakery::find().all(&db).await?;
assert_eq!(bakeries.len(), 1);

// Finding by id is built-in
let sad_bakery: Option<bakery::Model> = Bakery::find_by_id(1).one(db).await?;
let sad_bakery: Option<bakery::Model> = Bakery::find_by_id(1).one(&db).await?;
assert_eq!(sad_bakery.unwrap().name, "Sad Bakery");

// Finding by arbitrary column with `filter()`
let sad_bakery: Option<bakery::Model> = Bakery::find()
.filter(bakery::Column::Name.eq("Sad Bakery"))
.one(db)
.one(&db)
.await?;
assert_eq!(sad_bakery.unwrap().id, 1);
```
Expand All @@ -105,14 +105,14 @@ let john = chef::ActiveModel {
id: ActiveValue::Set(1), // The primary key must be set
..Default::default()
};
john.delete(db).await?;
john.delete(&db).await?;

let sad_bakery = bakery::ActiveModel {
id: ActiveValue::Set(1), // The primary key must be set
..Default::default()
};
sad_bakery.delete(db).await?;
sad_bakery.delete(&db).await?;

let bakeries: Vec<bakery::Model> = Bakery::find().all(db).await?;
let bakeries: Vec<bakery::Model> = Bakery::find().all(&db).await?;
assert!(bakeries.is_empty());
```