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

Many2Many fails when Creating object that uses UUID for ID #6208

Open
onurhuseyincantay opened this issue Apr 3, 2023 · 6 comments
Open
Assignees
Labels
type:with reproduction steps with reproduction steps

Comments

@onurhuseyincantay
Copy link

GORM Playground Link

go-gorm/playground#588

Description

When the primary key is a UUID that is generated on the database create object can't find primaryKey Field.

as mentioned at #5320

@a631807682
Copy link
Member

https://gorm.io/docs/query.html#Retrieving-objects-with-primary-key

If the primary key is a string (for example, like a uuid), the query will be written as follows:

db.First(&user, "id = ?", "1b74413f-f3b8-409f-ac47-e8c062e3472a")
// SELECT * FROM users WHERE id = "1b74413f-f3b8-409f-ac47-e8c062e3472a";

When the type is string, we cannot determine whether the user is using the primary key or the condition of the sql statement.

@onurhuseyincantay
Copy link
Author

@a631807682 I was referring for object creation I reused the existing Create function on the test case this is after creation.

@onurhuseyincantay
Copy link
Author

@a631807682 after some investigation I made my local version work and the reasoning is below:

// Fails
type User struct {
Languages []Language `gorm:"many2many:Languages"`
}

// Succeeds
type User struct {
Languages []Language `gorm:"many2many:UserSpeaks"`
}

so what happens is that the many2many table name has to be unique even a plural version of the table doesn't work.

@onurhuseyincantay
Copy link
Author

CC: @jinzhu,
since @a631807682 is on vacation.

@a631807682 a631807682 reopened this Apr 4, 2023
@a631807682
Copy link
Member

https://gorm.io/docs/conventions.html#Pluralized-Table-Name
I didn't understand your question. By default, a table with a plural name will be created, indicating that it cannot have the same name as a relational table.

@onurhuseyincantay
Copy link
Author

That I realized, but dont you think many2many relationship based variable would most probably also named as plural and this will cause error. I would suggest to not even migrate a table and check if the many2many table name on the flags is correct so the consumer of gorm can identify the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:with reproduction steps with reproduction steps
Projects
None yet
Development

No branches or pull requests

3 participants