More example
This commit is contained in:
parent
e30ce4932b
commit
16e1342e9a
@ -1,11 +1,18 @@
|
|||||||
# SeaORM SQLx MySql example
|
# 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:
|
Running:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
cargo run
|
cargo run
|
||||||
```
|
```
|
||||||
|
|
||||||
Example output:
|
Example output:
|
||||||
|
|
||||||
```sh
|
```sh
|
||||||
Database { connection: SqlxMySqlPoolConnection }
|
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" }
|
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
|
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) }
|
Model { id: 1, name: "Blueberry", cake_id: Some(1) }
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
use sea_orm::{Database, EntityTrait, QueryErr};
|
use sea_orm::{ColumnTrait, Database, EntityTrait, QueryErr};
|
||||||
|
|
||||||
mod example_cake;
|
mod example_cake;
|
||||||
mod example_fruit;
|
mod example_fruit;
|
||||||
@ -57,6 +57,17 @@ async fn find_one(db: &Database) -> Result<(), QueryErr> {
|
|||||||
println!("{:?}", cheese);
|
println!("{:?}", cheese);
|
||||||
println!();
|
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: ");
|
print!("find models belong to: ");
|
||||||
|
|
||||||
let fruits = cheese.find_fruit().all(db).await?;
|
let fruits = cheese.find_fruit().all(db).await?;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user