aqueduct icon indicating copy to clipboard operation
aqueduct copied to clipboard

How to save many-to-many?

Open rafante opened this issue 4 years ago • 2 comments

Hi! How should I save related objects? I mean, thinking about a post for example that has many tags, and a tag could be in many posts. Should I create the Tag(), the Post() save them in a transaction and then create a TagPost() with the tag and the post fields? Because I've tried something similar in other situation, but turns out that one of the fields of the TagPost (the tag or the post) stay null on the database even the Tag and the Post being saved and the documentation for many-to-many is a little poor on the website although I should say that in general the documentation for aqueduct is awesome and the project itself either (loving it, thank you by the way)

rafante avatar Apr 07 '20 15:04 rafante

Can you provide the queries you are writing?

Reductions avatar Apr 07 '20 18:04 Reductions

Hey @Reductions I have similar question here a example for one-to-many relationship:

class Author extends ManagedObject<_Author> implements _Author {}
class _Author {
  @primaryKey
  int id;
  String name;
  ManagedSet<Book> books;
}

class Book extends ManagedObject<_Book> implements _Book {}
class _Book {
  @primaryKey
  int id;
  String name;
  @Relate(#books)
  Author author;
}

I want to post Author with Books with that json body:

{
    "name": "author11",
    "books": [
      {"name": "book1"},
      {"name": "book2"}
    ]
}

How can I do that post easily without define new model type? What is the best way to do this?

Same question for many-to-many post.

YTarikKalyoncu avatar Sep 07 '20 09:09 YTarikKalyoncu