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

fix: Fixed the condition to use the default value #409

Open
wants to merge 5 commits into
base: main
Choose a base branch
from

Conversation

kemokemo
Copy link

@kemokemo kemokemo commented Nov 3, 2019

This PR is for correcting #408 .

The conditions for using the default value specified in the tag have been modified as follows.

before

If there is a default value specified by the tag, always insert it.

this fix

Only if the specified value is nil or Zero value, if there is a default value specified by the tag, it is inserted.

@nelsam
Copy link
Member

nelsam commented Dec 2, 2019

@kemokemo can you add tests?

@kemokemo
Copy link
Author

kemokemo commented Dec 3, 2019

@jbuchbinder Thank you for your approval.

@nelsam Thank you for your comment. I'll try it. Please give me some time.

@kemokemo
Copy link
Author

@nelsam Sorry for delay. Would you please confirm adding the test?

@nelsam
Copy link
Member

nelsam commented Dec 14, 2019

I see it. Thanks! I'm going to pull it down locally and do some extra verification. Something is nagging at the back of my mind about the FieldByName method ... it might have some scenarios where it doesn't work 🤔

Yeah, I'll have to double check. I'm a bit busy today, but I'll set reminders so hopefully I will check it out today or tomorrow.

@Pomyk
Copy link

Pomyk commented Jan 7, 2020

This code is not correct. Insert plan depends on the first row inserted. If that row uses default value all inserts will have default value.
You could use something like:
CASE WHEN isnull(value) OR value = zerovalue THEN default ELSE value END

Current code also uses reflection for each column even when default value is not set.

@kemokemo
Copy link
Author

@Pomyk Thank you so much for your review. I'll try to fix issues. 👍

val := elem.FieldByName(col.fieldName).Interface()
s2.WriteString(
fmt.Sprintf("case when %t or %s = %s then %s else %s end",
val == nil, t.dbmap.Dialect.BindVar(x), getZeroValueStringForSQL(val), col.DefaultValue, t.dbmap.Dialect.BindVar(x)))
Copy link

Choose a reason for hiding this comment

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

Using result of val == nil here doesn't seem right. You will end up with a plan that has case when true or ... or case when false or ...

Copy link
Author

Choose a reason for hiding this comment

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

Sorry. It's my wrong push. 🙇
I'm still working on the fix...

@kemokemo
Copy link
Author

@Pomyk
Hi! It took a while, but I managed to fix it. 🎉
I have fixed the number of the query's argument issue and could not determine data type of parameter error issue by above commit ( Please refer to the this issue ). To solve the latter problem, I thought we needed to add an interface.

I would be grateful if you could comment.

@Pomyk
Copy link

Pomyk commented Jan 17, 2020

I'm not a fan of this feature in an orm. The code looks good but I didn't have time to test it yet.

@mattn
Copy link

mattn commented Aug 5, 2021

Why this is not merged? This worked on v1. This is an obvious degrade.

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.

5 participants