Skip to content
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 with Custom column Id doesn't work #621

Closed
itsbalamurali opened this issue Mar 20, 2022 · 8 comments · Fixed by #694
Closed

Insert with Custom column Id doesn't work #621

itsbalamurali opened this issue Mar 20, 2022 · 8 comments · Fixed by #694
Labels
good first issue Good for newcomers

Comments

@itsbalamurali
Copy link

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
Copy link
Member

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
Copy link

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
Copy link
Member

tyt2y3 commented Mar 31, 2022

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

@tyt2y3 tyt2y3 added the good first issue Good for newcomers label Mar 31, 2022
@KaviiSuri
Copy link

Can I pick this issue if it's free?

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 3, 2022

@KaviiSuri sure, that'd definitely be appreciated

@KaviiSuri
Copy link

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

@tyt2y3
Copy link
Member

tyt2y3 commented Apr 3, 2022

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

@billy1624 billy1624 linked a pull request May 18, 2022 that will close this issue
1 task
@billy1624
Copy link
Member

Fixed on #694

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
good first issue Good for newcomers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants