SeaQL / sea-orm

🐚 An async & dynamic ORM for Rust
https://www.sea-ql.org/SeaORM/
Apache License 2.0
7.3k stars 513 forks source link

Insert with Custom column `Id` doesn't work #621

Closed itsbalamurali closed 2 years ago

itsbalamurali commented 2 years ago

Description

Insert with Custom column Id doesn't work.

I have an entity with a custom column name Id instead of id.

Steps to Reproduce

#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Serialize, Deserialize)]
#[sea_orm(table_name = "Somethings")]
struct SomethingModel {
    #[sea_orm(column_name = "Id", primary_key, auto_increment = false)]
    pub id: Uuid,
}

let something = ActiveModel{}... //create active model and set values...

Something::Entity::insert(something)
         .exec(db)
         .await
         .unwrap();

Expected Behavior

The above should generate


INSERT INTO
  \"Somethings\" (
    \"Id\"
  )
VALUES
  ($1) RETURNING \"Id\"

Actual Behavior


INSERT INTO
  \"Somethings\" (
    \"Id\"
  )
VALUES
  ($1) RETURNING \"id\"

Causing:

thread 'tokio-runtime-worker' panicked at 'called `Result::unwrap()` on an `Err` value: Query("error returned from database: column \"id\" does not exist")'

Reproduces How Often

Always.

Versions

── sea-orm v0.6.0
│   ├── sea-orm-macros v0.6.0 (proc-macro)
│   ├── sea-query v0.21.0
│   │   ├── sea-query-derive v0.2.0 (proc-macro)
│   ├── sea-strum v0.23.0
│   │   └── sea-strum_macros v0.23.0 (proc-macro)

OS: MacOSx. Database: PostgreSQL 13.

billy1624 commented 2 years ago

Hey @itsbalamurali, welcome! I can't reproduce your error.

Can you show me your entity file?

use sea_orm::entity::prelude::*;

#[derive(Clone, Debug, PartialEq, DeriveEntityModel)]
#[sea_orm(table_name = "metadata")]
pub struct Model {
    #[sea_orm(primary_key, column_name = "Uuid", auto_increment = false)]
    pub uuid: Uuid,
    #[sea_orm(column_name = "type", enum_name = "Type")]
    pub ty: String,
    pub key: String,
    pub value: String,
    pub bytes: Vec<u8>,
    pub date: Option<Date>,
    pub time: Option<Time>,
}

#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}

impl ActiveModelBehavior for ActiveModel {}
let metadata = metadata::Model {
    uuid: Uuid::new_v4(),
    ty: "Type".to_owned(),
    key: "markup".to_owned(),
    value: "1.18".to_owned(),
    bytes: vec![1, 2, 3],
    date: Some(Date::from_ymd(2021, 9, 27)),
    time: Some(Time::from_hms(11, 32, 55)),
};

let result = metadata.clone().into_active_model().insert(db).await?;
INSERT INTO "metadata" ("Uuid", "type", "key", "value", "bytes", "date", "time")
VALUES ('0b2e6e69-7d2b-43ad-968e-8eb60dd54a26', 'Type', 'markup', '1.18', '010203', '2021-09-27', '11:32:55')
RETURNING "Uuid", "type", "key", "value", "bytes", "date", "time"
DanielJoyce commented 2 years ago

Your column name is not "Id", so you're not even testing the same bug. Perhaps there is accidentally some special case handling for "id" columns?

tyt2y3 commented 2 years ago

This sounds like a bug in how we handle the returning part of a query

KaviiSuri commented 2 years ago

Can I pick this issue if it's free?

tyt2y3 commented 2 years ago

@KaviiSuri sure, that'd definitely be appreciated

KaviiSuri commented 2 years ago

Thanks, can you tell me where should I start looking? Would love a little guidance!

tyt2y3 commented 2 years ago

Well I think reproducing the issue in a test case would be the first step

billy1624 commented 2 years ago

Fixed on #694