Insert an active model and get back the last insert id using static method
let pear = fruit::ActiveModel {
..Default::default() // just only autoincremented id here
};
let res: InsertResult = Fruit::insert(pear).exec(db).await?;
### Expected Behavior
It must insert and return saved id
### Actual Behavior
It leads an error:
"Custom Error: Custom Error: Attribute id is NotSet"
### Reproduces How Often
All the time
### Workarounds
Don't use static method and use such one instead:
```rust
let pear = fruit::ActiveModel {
..Default::default()
};
let pear: fruit::Model = pear.insert(db).await?;
3. Generate a model based on migration above
3. Then try to insert entity using generated model:
```rust
let pear = fruit::ActiveModel {
..Default::default() // just only autoincremented id here
};
let res: InsertResult = Fruit::insert(pear).exec(db).await?;
Description
Insert an active model and get back the last insert id doesn't work with autoincrement id.
Steps to Reproduce
let res: InsertResult = Fruit::insert(pear).exec(db).await?;
"Custom Error: Custom Error: Attribute id is NotSet"
Reproducible Example
[derive(DeriveMigrationName)]
pub struct Migration;
[async_trait::async_trait]
impl MigrationTrait for Migration { async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { manager .create_table( Table::create() .table(Fruit::Table) .if_not_exists() .col( ColumnDef::new(Fruit::Id) .integer() .not_null() .auto_increment() .primary_key(), ) .to_owned(), ) .await }
}
[derive(DeriveIden)]
enum Fruit { Table, Id, }
Versions
0.12.14