ImportableUniqueObjects and to-many relationship #366

Closed
opened 2025-12-29 15:30:05 +01:00 by adam · 1 comment
Owner

Originally created by @Maxtws on GitHub (Jun 23, 2021).

Hi!
I have 10 unique objects (Category) stored in database. Is it possible to link some of them to multiple entities? Here're my entities:

class Category: CoreStoreObject, ImportableUniqueObject {

    @Field.Stored("id")
    var id: Int = 0

    @Field.Stored("name")
    var name: String = ""

    @Field.Stored("color")
    var color: String = ""

    @Field.Relationship("post", inverse: \.$categories)
    var post: Post?
}

And the other entity that has to-many reference on Category:

class Post: CoreStoreObject, ImportableUniqueObject {

    @Field.Stored("id")
    var id: Int = 0

    @Field.Stored("title")
    var title: String = ""

    @Field.Relationship("categories")
    var categories: [Category]
}

By the time I create Post object, all 10 Category objects are already persisted in DB.
ImportSource for the Post entity has only list of category IDs. So I fetch category entities from DB and assign them to categories like this in didInsert(...) method in Post class:

   let categories: [Category] = try transaction.fetchAll(From<Category>().where(format: "id IN %@", argumentArray: [categoriesIDs]))  
   self.categories = categories

However, while printing all posts I can only see some of them have categories filled. In fact I can see only unique Category objects appear in print.

How do I set up my entities relationships to be able to have many categories in categories and have only 10 unique Category objects?

Originally created by @Maxtws on GitHub (Jun 23, 2021). Hi! I have 10 unique objects (Category) stored in database. Is it possible to link some of them to multiple entities? Here're my entities: ``` class Category: CoreStoreObject, ImportableUniqueObject { @Field.Stored("id") var id: Int = 0 @Field.Stored("name") var name: String = "" @Field.Stored("color") var color: String = "" @Field.Relationship("post", inverse: \.$categories) var post: Post? } ``` And the other entity that has to-many reference on Category: ``` class Post: CoreStoreObject, ImportableUniqueObject { @Field.Stored("id") var id: Int = 0 @Field.Stored("title") var title: String = "" @Field.Relationship("categories") var categories: [Category] } ``` By the time I create Post object, all 10 Category objects are already persisted in DB. ImportSource for the Post entity has only list of category IDs. So I fetch category entities from DB and assign them to `categories` like this in `didInsert(...)` method in Post class: ``` let categories: [Category] = try transaction.fetchAll(From<Category>().where(format: "id IN %@", argumentArray: [categoriesIDs])) self.categories = categories ``` However, while printing all posts I can only see some of them have `categories` filled. In fact I can see only unique Category objects appear in print. How do I set up my entities relationships to be able to have many categories in `categories` and have only 10 unique Category objects?
adam closed this issue 2025-12-29 15:30:05 +01:00
Author
Owner

@Maxtws commented on GitHub (Jun 25, 2021):

It seems like this might be similar to https://github.com/JohnEstropia/CoreStore/issues/229
However I'm not sure I understand this solution: "relationships should have the inverse relationship set on both entities"?

@Maxtws commented on GitHub (Jun 25, 2021): It seems like this might be similar to https://github.com/JohnEstropia/CoreStore/issues/229 However I'm not sure I understand this solution: "relationships should have the inverse relationship set on both entities"?
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/CoreStore#366