Strategy to handle nested records #1386
-
Hello! struct A: PersistableRecord, FetchableRecord, TableRecord {
let id: String
var b: B
}
struct B: PersistableRecord, FetchableRecord, TableRecord {
let id: String
let value: Int
} So |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Hello @tgrapperon, I don't doubt that the
But And Using unfit protocols will create mismatches and oddities that are the signs that the application is fighting the framework. All in all, the For further information, and details about the setup expected by GRDB, see the related sections in the Recommended Practices for Designing Record Types guide: I'm not saying this is ideal, and there is certainly room for improvements. Previous issues shed some light on the problems to solve: #1322, #1354. |
Beta Was this translation helpful? Give feedback.
Hello @tgrapperon,
I don't doubt that the
A
type is the one you want your app to use. It's the "canonical" model. My advice is to not make it use unfit protocols.A
can implementFetchableRecord
(please refer to its documentation, as well as the Associations guide).But
A
is not a "table record", becauseTableRecord
is all about single tables. For example, whenTableRecord
generates SQL forA.fetchAll(db)
, it does not embed the Bs.And
A
is not a "persistable record" either. If you think about it, persisting a composite object such asA
supports multiple strategies. For example, what deleting anA
should do? Maybe your app wants to delete rows in both the A and B tables. Maybe some other …