-
-
Notifications
You must be signed in to change notification settings - Fork 454
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
Insert to mysql, can find data inserted in mysql but the method returns with Err(RecordNotFound("Failed to find inserted item")). #2139
Comments
I encountered this bug yesterday and spent a few hours troubleshooting it Tl;Dr: Either use The but sea_orm doesn't account for this, assuming that the last_insert_id is valid: sea-orm/src/executor/insert.rs Lines 294 to 297 in 010433b
Given that there's separate The simple fix is to use this is what I'm doing: let new_user_id = rand::random::<u64>();
let user = ActiveModel {
id: Set(new_user_id),
email: Set(email.clone()),
password_hash: Set(password_hash.clone())
};
Entity::insert(user).exec(conn).await?;
let new_user = Entity::find_by_id(new_user_id).one(conn).await?.ok_or(anyhow!("inserted user not found"))?;
return Ok(new_user); The more complex fix is to make sure your primary key column is set to auto-increment so mysql will actually return the inserted ID. or just switch to postgres which doesn't seem to have this issue. |
Description
hi, I am developing the backend of an app with axum and sea-orm, so there is a route to register a user, and insert the user to databse, i am using mysql as my database, but any time i call this route, it returns with Err(RecordNotFound("Failed to find inserted item")), however, i can find data in my database.
Steps to Reproduce
the below is the router
the below is the database operator, the res is Err(RecordNotFound("Failed to find inserted item")).
Expected Behavior
returns the Model.
Actual Behavior
returns err Err(RecordNotFound("Failed to find inserted item"))
Reproduces How Often
every time
Workarounds
Reproducible Example
Versions
sqlx = "0.7"
sea-orm = { version = "0.12", features = [
"sqlx-mysql",
"runtime-tokio-native-tls",
"macros",
"with-chrono",
] }
The text was updated successfully, but these errors were encountered: