Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

gebes
Copy link

@gebes gebes commented Aug 10, 2024

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 like created_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!

will := client.Student.Create().SetName("Will").SaveX(ctx)

math := client.Subject.Create().SetName("Math").
AddStudents(steve, bob).
Copy link
Author

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

Copy link
Author

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

@gebes
Copy link
Author

gebes commented Aug 10, 2024

@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" . }}
Copy link
Member

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.

Copy link
Author

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
Copy link
Member

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.

@Southclaws
Copy link

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!

@rubensayshi
Copy link
Contributor

rubensayshi commented Feb 7, 2025

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!

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.

4 participants