-
-
Notifications
You must be signed in to change notification settings - Fork 245
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 eager creation for associations by adding where clause #607
fix eager creation for associations by adding where clause #607
Conversation
@stanislas-m @markbates any chance to review or any suggestion? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you, could you please add a failing test case first which shows the behaviour you describe - first to verify your findings and second to verify your fix!
@@ -265,7 +265,8 @@ func (c *Connection) Create(model interface{}, excludeColumns ...string) error { | |||
if IsZeroOfUnderlyingType(id) { | |||
return c.Create(m.Value) | |||
} | |||
exists, errE := Q(c).Exists(i) | |||
|
|||
exists, errE := Q(c).Where("id = ?", id).Exists(i) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The ID column is variable depending on the struct and should therefore not be hardcoded. m
has a function whereID
which should be used instead.
Hello @xixinjie, Thank you for reporting this issue and contribution. I took a look at the issue and definitely the original usage was problematic. Please resolve the review comment, then I will take care of the next steps along with test cases. I am not sure if you still have an interest in this issue. If you do not respond, I will take over this issue. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
minor fix will be on another PR.
Problem
When try to use
db.Eager().ValidateAndCreate(user)
to create a user and underlyinghas_many
books, it succeeded for the first time when books table is empty, but failed to save the books in following creations.How to fix
Add where clause when check if the books exist as shown in the PR.
Samples
SQL logs: