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

feat: Add embed field attribute for AsChangeset #2529

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

heyrict
Copy link
Contributor

@heyrict heyrict commented Oct 7, 2020

Closes #2527

@TaKO8Ki TaKO8Ki requested a review from a team October 7, 2020 05:14
Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for working on this. This requires at least the following changes:

  • Add the corresponding documentation on top of the derive_as_changeset method
  • Add at least one test case showing that this works

EDIT: I try to find some time to have a look at the CI errors. It seems like a regression in rustc 😞
EDIT2: Opened #2531 to fix the CI issues.

Copy link
Member

@weiznich weiznich left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update. Left 1 minor style comments + CI should pass.

diesel_tests/tests/update.rs Outdated Show resolved Hide resolved
name: None,
hair_color: HairColor::Black,
})
.get_result(&connection)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Better use a separate query here to load the results, as both Sqlite and Mysql do not support queries containing RETURNING clauses. (Or use the UpdateAndFetchResult trait impls.)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the hint! I was wandering about the error messages here.

@heyrict
Copy link
Contributor Author

heyrict commented Oct 9, 2020

Well, I'm a little confused about the syntax error at or near "=" error, which seems to be an error originated from sql generation. I may take some more time looking into this.

@weiznich
Copy link
Member

@heyrict I would probably try to dump the generate SQL via diesel::debug_query and than look there what's wrong.

@heyrict
Copy link
Contributor Author

heyrict commented Oct 16, 2020

Thanks for the hint!

I didn't touch the implementation of Eq<Left, Right> though. I'm not sure why it is generating an invalid sql. I'll try to fix that later.

UPDATE "users" SET ("users"."hair_color" = $1) WHERE ("users"."id" = $2)
-- instead of
UPDATE "users" SET "hair_color" = $1  WHERE ("users"."id" = $2)

let field_ty = &field.ty;
parse_quote!(diesel::dsl::Eq<#table_name::#column_name, #lifetime #field_ty>)
parse_quote!(#lifetime #field_ty)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From looking again at this here I think we must call <#lifetime #field_ty as AsChangeSet>::Changeset here`

if lifetime.is_some() {
parse_quote!(self#field_access.as_ref().map(|x| #table_name::#column_name.eq(x)))
if field.has_flag("embed") {
parse_quote!(#lifetime self#field_access)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should then be <#lifetime #field_ty as AsChangeset>::as_changeset(self#field_access)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I'll try that this weekend.

@pksunkara pksunkara assigned pksunkara and unassigned pksunkara Jun 30, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Field annotation for interpreting item with user-provided function in AsChangeset
3 participants