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?
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"?
Blocking a user prevents them from interacting with repositories, such as opening or commenting on pull requests or issues. Learn more about blocking a user.
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:
And the other entity that has to-many reference on 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
categorieslike this indidInsert(...)method in Post class:However, while printing all posts I can only see some of them have
categoriesfilled. 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
categoriesand have only 10 unique Category objects?@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"?