-
-
Notifications
You must be signed in to change notification settings - Fork 464
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1403 from /issues/1357
Don't call last_insert_id if not needed
- Loading branch information
Showing
8 changed files
with
116 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
[workspace] | ||
# A separate workspace | ||
|
||
[package] | ||
name = "sea-orm-issues-1357" | ||
version = "0.1.0" | ||
edition = "2021" | ||
publish = false | ||
|
||
[dependencies] | ||
anyhow = "1" | ||
serde = "1" | ||
tokio = { version = "1", features = ["rt", "rt-multi-thread", "macros"] } | ||
|
||
[dependencies.sea-orm] | ||
path = "../../" | ||
default-features = false | ||
features = ["macros", "runtime-tokio-native-tls", "sqlx-sqlite"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
use sea_orm::entity::prelude::*; | ||
|
||
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)] | ||
#[sea_orm(table_name = "pool")] | ||
pub struct Model { | ||
#[sea_orm(primary_key, auto_increment = false)] | ||
pub id: i64, | ||
pub name: String, | ||
} | ||
|
||
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] | ||
pub enum Relation {} | ||
|
||
impl ActiveModelBehavior for ActiveModel {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
use anyhow::Result; | ||
use sea_orm::{ConnectionTrait, Database, EntityTrait, IntoActiveModel, Schema}; | ||
|
||
mod entity; | ||
|
||
use entity::*; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<()> { | ||
let db = Database::connect("sqlite::memory:").await.unwrap(); | ||
|
||
let builder = db.get_database_backend(); | ||
let schema = Schema::new(builder); | ||
let stmt = schema.create_table_from_entity(Entity); | ||
db.execute(builder.build(&stmt)).await?; | ||
|
||
let model = Model { | ||
id: 100, | ||
name: "Hello".to_owned(), | ||
}; | ||
|
||
let res = Entity::insert(model.clone().into_active_model()) | ||
.exec(&db) | ||
.await?; | ||
|
||
assert_eq!(Entity::find().one(&db).await?, Some(model.clone())); | ||
assert_eq!(res.last_insert_id, model.id); | ||
|
||
let model = Model { | ||
id: -10, | ||
name: "World".to_owned(), | ||
}; | ||
|
||
let res = Entity::insert(model.clone().into_active_model()) | ||
.exec(&db) | ||
.await?; | ||
|
||
assert_eq!(Entity::find().one(&db).await?, Some(model.clone())); | ||
assert_eq!(res.last_insert_id, model.id); | ||
|
||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters