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

[Question] Many2Many insert on new parent fails foreign key constraint #600

Open
paulinevos opened this issue Nov 1, 2022 · 1 comment
Assignees

Comments

@paulinevos
Copy link

Document Link

Your Question

It is unclear to me how, if at all, I can insert a new parent entity holding a nested many2many association while still being able to maintain a foreign key constraint on the association table, i.e.:

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

type Language struct {
  Key string
}

user := User{
  Languages: []Language{{Key: "nl-NL"}}
}

db.Save(&user)  // Will fail on an fk constraint violation for user ID, as the user does not exist yet.

To be clear I understand I could Append the languages after first saving the user, but imo it would be desired behavior to be able to create a new entity with nested entities directly instead of having to append them separately after the Save.

Expected answer

How to achieve this if at all possible, and if not, would it be possible to open a feature request

@paulinevos
Copy link
Author

Worth noting: if you agree I'd be happy to contribute this myself if it's missing :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants