related().attach() doesn't check existance of relation (many to many) #4065
-
I have this code to add new relationship: post.related('images').attach([imageId]); I thought that it would check existance before creating new relationship. But it throws error when inserting duplicate images:
Do I have to query existing relationships before adding new ones? |
Beta Was this translation helpful? Give feedback.
Answered by
RomainLanz
Feb 8, 2023
Replies: 1 comment
-
Hey @guoyunhe! 👋🏻 Use post.related('images').sync([imageId], false); 📚 https://docs.adonisjs.com/reference/orm/relations/many-to-many#sync |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
guoyunhe
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hey @guoyunhe! 👋🏻
Use
sync
instead.📚 https://docs.adonisjs.com/reference/orm/relations/many-to-many#sync