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

Update / Delete by id #552

Closed
Ilqjx opened this issue Feb 21, 2022 · 6 comments · Fixed by #590
Closed

Update / Delete by id #552

Ilqjx opened this issue Feb 21, 2022 · 6 comments · Fixed by #590
Labels
good first issue Good for newcomers

Comments

@Ilqjx
Copy link

Ilqjx commented Feb 21, 2022

Is there something like find_by_id(1).one(&db) for update and delete?

Look like update_by_id(1).one(&db) and delete_by_id(1).one(&db).

@billy1624
Copy link
Member

Hey @Ilqjx, interesting idea!

For delete, I think it's make sense to delete by id. I support adding a new API for it.

For update, I would say it's natural to select the ActiveModel from db then alter the row then save it back to db.

let orange: Option<fruit::Model> = Fruit::find_by_id(30).one(db).await?;
let orange: fruit::Model = orange.unwrap();

let res: DeleteResult = orange.delete(db).await?;
assert_eq!(res.rows_affected, 1);

Or, update any rows exists that match specific conditions.

// Bulk set attributes using ActiveModel
let update_result: UpdateResult = Fruit::update_many()
    .set(pear)
    .filter(fruit::Column::Id.eq(1))
    .exec(db)
    .await?;

// UPDATE `fruit` SET `cake_id` = NULL WHERE `fruit`.`name` LIKE '%Apple%'
Fruit::update_many()
    .col_expr(fruit::Column::CakeId, Expr::value(Value::Null))
    .filter(fruit::Column::Name.contains("Apple"))
    .exec(db)
    .await?;

I'm not sure how update_by_id(1).one(&db) would work? Can you explain more with it?

@Ilqjx
Copy link
Author

Ilqjx commented Mar 4, 2022

Indeed, delete is useful and update is unnecessary.

@billy1624 billy1624 added the good first issue Good for newcomers label Mar 8, 2022
@billy1624
Copy link
Member

I will open this issue for contributions :)

@AbhijithGanesh
Copy link

I'd like to work on this @billy1624

@billy1624
Copy link
Member

Hey @AbhijithGanesh, thanks for the interest!!

The delete_by_id API is under development by @ShouvikGhosh2048.

I saw your message on Discord, you want to work on the update_by_id API. However, as discussed above. Seems that it's not necessary to have the API. I wonder if you got any other thoughts on it?

@AbhijithGanesh
Copy link

I agree with llqjx

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.

3 participants