More example

This commit is contained in:
Chris Tsang 2021-05-10 03:16:39 +08:00
parent e30ce4932b
commit 16e1342e9a
2 changed files with 23 additions and 1 deletions

View File

@ -1,11 +1,18 @@
# SeaORM SQLx MySql example
Prepare:
Setup a test database and configure the connection string in `main.rs`.
Run `bakery.sql` to setup the test table and data.
Running:
```sh
cargo run
```
Example output:
```sh
Database { connection: SqlxMySqlPoolConnection }
@ -27,6 +34,10 @@ find one by primary key: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `ca
Model { id: 1, name: "New York Cheese" }
find one by like: SELECT `cake`.`id`, `cake`.`name` FROM `cake` WHERE `cake`.`name` LIKE '%chocolate%' LIMIT 1
Model { id: 2, name: "Chocolate Fudge" }
find models belong to: SELECT `fruit`.`id`, `fruit`.`name`, `fruit`.`cake_id` FROM `fruit` INNER JOIN `cake` ON `cake`.`id` = `fruit`.`cake_id` WHERE `cake`.`id` = 1
Model { id: 1, name: "Blueberry", cake_id: Some(1) }

View File

@ -1,4 +1,4 @@
use sea_orm::{Database, EntityTrait, QueryErr};
use sea_orm::{ColumnTrait, Database, EntityTrait, QueryErr};
mod example_cake;
mod example_fruit;
@ -57,6 +57,17 @@ async fn find_one(db: &Database) -> Result<(), QueryErr> {
println!("{:?}", cheese);
println!();
print!("find one by like: ");
let chocolate = cake::Entity::find()
.filter(cake::Column::Name.contains("chocolate"))
.one(db)
.await?;
println!();
println!("{:?}", chocolate);
println!();
print!("find models belong to: ");
let fruits = cheese.find_fruit().all(db).await?;