Comments (1)
I encountered this bug yesterday and spent a few hours troubleshooting it
Tl;Dr: Either use Entity::insert(user).exec(conn)
or make the table's primary key AUTO_INCREMENT.
The ActiveModel.insert
function attempts to return the freshly-inserted row if it was told that the insert was successful. If the database supports returning inserted rows then it will convert the row result to a model and return it. If it doesn't, it uses the "last_insert_id" field of the result to look up the row it just inserted. MySQL doesn't set the last_insert_id on the insert result unless the primary key of the table is auto_increment:
https://dev.mysql.com/doc/refman/8.0/en/information-functions.html#function_last-insert-id
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 insert_with_returning
and insert_without_returning
functions, I have to assume this is expected behavior but I still wish this function could handle this error more gracefully or more obviously, or at least document it somehow/somewhere?
The simple fix is to use Entity::insert(user).exec(conn)
which instead returns the insert result, which, if Ok
, contains last_insert_id
but it's set to 0. Doesn't really matter since you know the insert succeeded and you should already have the ID you set in the active model so you can just follow it up with a Entity::find_by_id(id).one(conn)
to get the fresh model.
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.
from sea-orm.
Related Issues (20)
- Insert an active model and get back the last insert id doesn't work with autoincrement
- Enum column named "model" generates code that conflicts with the struct Model
- ambiguous associated type when access Entity::Column HOT 5
- TimestampWithTimeZone returned as array
- Allow Defaulting `string_value` For ActiveEnum Based On Renaming Rules On Variants HOT 1
- transaction use probelm with state
- Sea-ORM still includes sqlx-mysql dependency even when no mysql feature is enabled
- sea-orm-migration - Compile error using most recent version of rc2 HOT 12
- Missing downward feature `runtime-tokio`. HOT 4
- Mock Auto Increment ID Not Working for Postgres Backend Complaining About Null HOT 1
- sea-orm-cli generate error HOT 1
- .money_len type creates invalid SQL HOT 1
- Compilation error when building with sqlx-sqlite and runtime-async-std-rustls HOT 3
- is Sea orm Json type can't insert text [] type HOT 2
- Migration fresh command cannot drop types with Postgresql
- Clarification on Relationship Generation in sea-orm-cli Based on ForeignKey Constraints
- `find_with_related` implicitly adds ordering on `left.id` HOT 3
- In migration use enumeration
- Expose `order_by_with_nulls` in `QueryOrder` trait HOT 3
- Alter enum column with new type (missing USING)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from sea-orm.