-
Notifications
You must be signed in to change notification settings - Fork 986
M2M Bulk through add insertion bug for #4130 #4170
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
base: master
Are you sure you want to change the base?
Conversation
will := client.Student.Create().SetName("Will").SaveX(ctx) | ||
|
||
math := client.Subject.Create().SetName("Math"). | ||
AddStudents(steve, bob). |
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.
@a8m the main issue was, that when adding multiple students to a M2M edge which have a through table, and the through table has a default id, then the insertion fails because all of the ids were reused
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.
Without this fix you will get for this test:
panic: ent: constraint failed: add m2m edge for table subject_students: UNIQUE constraint failed: subject_students.id [recovered]
panic: ent: constraint failed: add m2m edge for table subject_students: UNIQUE constraint failed: subject_students.id
@a8m requesting a review for this example. I created completely new tables, to avoid interfering with the already existing tests and to keep coverage up |
{{- if $e.Through }}for _, node := range nodes { {{end}} | ||
{{- $hasThrough := ne $e.Through nil}} | ||
{{- with extend $ "Edge" $e "Nodes" (not $hasThrough) "Zero" $zero "Through" ($hasThrough) }} | ||
{{ template "dialect/sql/defedge" . }} |
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.
I think we can change sqlgraph
to accept a Value
function instead of generating a spec per object - I'll check what's the simplest way to do it.
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.
Any update?
// This source code is licensed under the Apache 2.0 license found | ||
// in the LICENSE file in the root directory of this source tree. | ||
|
||
package schema |
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.
Edge-schemas are tested in integration/edgeschema
. See this example test: https://github.com/ent/ent/blob/master/entc/integration/edgeschema/edgeschema_test.go#L184
I prefer to keep edge-schema tests on the same directory, and reuse existing schemas if possible.
I've just recently run into this and spend quite a bit of time debugging, didn't notice the issue and this PR. Would love to see this revisited! |
hitting this issue as well, it's really nasty when you use UUIDs for IDs because it will violate the unique constraints of the primary key! |
This PR fixes issue #4130, which involved adding multiple M2M rows to a table using
Through
tables. Previously, the same default values were reused for every row, which was fine for fields likecreated_at
with a default date. However, this caused issues when using a DefaultID generator, leading to potential duplicate primary key errors.With this update, the affected
Through
edges are modified to ensure the "default" builder is called for each new row, preventing these errors.This change has been tested in several production projects, and all tests are passing.
Thanks for taking a look!