{"action":"created","issue":{"url":"https://api.github.com/repos/groue/GRDB.swift/issues/352","repository_url":"https://api.github.com/repos/groue/GRDB.swift","labels_url":"https://api.github.com/repos/groue/GRDB.swift/issues/352/labels{/name}","comments_url":"https://api.github.com/repos/groue/GRDB.swift/issues/352/comments","events_url":"https://api.github.com/repos/groue/GRDB.swift/issues/352/events","html_url":"https://github.com/groue/GRDB.swift/issues/352","id":322601636,"number":352,"title":"Associations and Inserts (GRDB 3)","user":{"login":"freak4pc","id":605076,"avatar_url":"https://avatars0.githubusercontent.com/u/605076?v=4","gravatar_id":"","url":"https://api.github.com/users/freak4pc","html_url":"https://github.com/freak4pc","followers_url":"https://api.github.com/users/freak4pc/followers","following_url":"https://api.github.com/users/freak4pc/following{/other_user}","gists_url":"https://api.github.com/users/freak4pc/gists{/gist_id}","starred_url":"https://api.github.com/users/freak4pc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freak4pc/subscriptions","organizations_url":"https://api.github.com/users/freak4pc/orgs","repos_url":"https://api.github.com/users/freak4pc/repos","events_url":"https://api.github.com/users/freak4pc/events{/privacy}","received_events_url":"https://api.github.com/users/freak4pc/received_events","type":"User","site_admin":false},"labels":[{"id":230659300,"url":"https://api.github.com/repos/groue/GRDB.swift/labels/question","name":"question","color":"cc317c","default":true}],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":7,"created_at":"2018-05-13T15:08:16Z","updated_at":"2018-05-16T12:17:08Z","closed_at":null,"author_association":"NONE","body":"I've been evaluating GRDB 3 and have failed to find documentation of how inserts work for associated objects. \r\n\r\nMy instinct was that if an object has a `hasOne(Other.self)`, and I insert the owning object, it would also take care of inserting the `Other` object, but I'm getting `fatalError`s, so I imagine I'm doing something wrong.\r\n\r\nMy objects look similar to this:\r\n\r\n```swift\r\nstruct Order: TableRecord, FetchableRecord {\r\n    static let store = hasOne(Store.self)\r\n\r\n    let store: Store\r\n    /// more fields\r\n}\r\n\r\nstruct Store: TableRecord, FetchableRecord {\r\n    static let orders = hasMany(Order.self)\r\n    \r\n    ... more fields\r\n\r\n    var orders: QueryInterfaceRequest<Order> {\r\n        return request(for: Store.orders)\r\n    }\r\n}\r\n```\r\n\r\nMy impression was that initializing the `Order` object with a `Store` set, and then doing `db.insert(store)` as part of a transaction should work, but I'm getting a `fatalError`. \r\n\r\nI couldn't find any documentation as to what should be filled in `init(row:)` or `encode(to:)`, perhaps those are the culprit. \r\n\r\nWould appreciate any help :) \r\n\r\nThanks."},"comment":{"url":"https://api.github.com/repos/groue/GRDB.swift/issues/comments/389497737","html_url":"https://github.com/groue/GRDB.swift/issues/352#issuecomment-389497737","issue_url":"https://api.github.com/repos/groue/GRDB.swift/issues/352","id":389497737,"user":{"login":"freak4pc","id":605076,"avatar_url":"https://avatars0.githubusercontent.com/u/605076?v=4","gravatar_id":"","url":"https://api.github.com/users/freak4pc","html_url":"https://github.com/freak4pc","followers_url":"https://api.github.com/users/freak4pc/followers","following_url":"https://api.github.com/users/freak4pc/following{/other_user}","gists_url":"https://api.github.com/users/freak4pc/gists{/gist_id}","starred_url":"https://api.github.com/users/freak4pc/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/freak4pc/subscriptions","organizations_url":"https://api.github.com/users/freak4pc/orgs","repos_url":"https://api.github.com/users/freak4pc/repos","events_url":"https://api.github.com/users/freak4pc/events{/privacy}","received_events_url":"https://api.github.com/users/freak4pc/received_events","type":"User","site_admin":false},"created_at":"2018-05-16T12:17:08Z","updated_at":"2018-05-16T12:17:08Z","author_association":"NONE","body":"Hey @groue, thanks for the detailed explanation. \r\n\r\nI don't feel I try to \"bend my way into making it a managed ORM\". I moved out of Realm specifically to avoid that promise since it doesn't fit the mental model I expect and this specific app doesn't benefit from Realm's feature-set. \r\n\r\nI do not plan on writing my own ORM but I do have a light abstraction layer on top in my implementation.\r\n\r\nRegarding your questions\r\n\r\n1. An order *must* have a store, it cannot exist without that info. \r\n2. I defined the `QueryInterfaceRequest` on orders just as a \"Starter\" when trying to look into GRDB. The question was how to create the reverse relationship, out of curiosity mainly - as I don't need it.\r\n\r\nSome of the constraints I defined in my question are theoretical. For example, I do not really need the inverse relationship (e.g. Order has many Store(s)), but I was interested in the syntax around that.\r\n\r\nAnyways, I think you solved my misunderstanding around this issue. I'm not trying to make things managed, but I am trying to make models properly connected and associated, so I don't have to manually use this \"less than optimal way\" of fetching an Order's store, for example. "}}
{"actor":{"display_login":"freak4pc"}}