From dadece1b04e0ee8c22dcaff5af2859ee9edc0dfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sun, 20 May 2018 19:35:18 +0200 Subject: [PATCH] Associations: inline documentation --- Documentation/AssociationsBasics.md | 2 +- .../Association/ForeignKey.swift | 58 ++++++++++++++++++- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/Documentation/AssociationsBasics.md b/Documentation/AssociationsBasics.md index 406dceb78..f137641d2 100644 --- a/Documentation/AssociationsBasics.md +++ b/Documentation/AssociationsBasics.md @@ -544,7 +544,7 @@ struct Person: TableRecord { } ``` -Foreign keys can also be defined from `Column`: +Foreign keys can also be defined from query interface columns: ```swift struct Book: TableRecord { diff --git a/GRDB/QueryInterface/Association/ForeignKey.swift b/GRDB/QueryInterface/Association/ForeignKey.swift index 032694e69..db2fca074 100644 --- a/GRDB/QueryInterface/Association/ForeignKey.swift +++ b/GRDB/QueryInterface/Association/ForeignKey.swift @@ -1,4 +1,60 @@ -/// A Foreign Key that helps building associations. +/// A ForeignKey helps building associations when GRDB can't infer a foreign +/// key from the database schema. +/// +/// Sometimes the database schema does not define any foreign key between two +/// tables. And sometimes, there are several foreign keys from a table +/// to another: +/// +/// Table book Table author +/// • id +--> • id +/// • authorId ------+ • name +/// • translatorId --+ +/// • title +/// +/// When this happens, associations can't be automatically inferred from the +/// database schema. GRDB will complain with a fatal error such as "Ambiguous +/// foreign key from book to author", or "Could not infer foreign key from book +/// to author". +/// +/// Your help is needed. You have to instruct GRDB which foreign key to use: +/// +/// struct Book: TableRecord { +/// // Define foreign keys +/// static let authorForeignKey = ForeignKey(["authorId"])) +/// static let translatorForeignKey = ForeignKey(["translatorId"])) +/// +/// // Use foreign keys to define associations: +/// static let author = belongsTo(Person.self, using: authorForeignKey) +/// static let translator = belongsTo(Person.self, using: translatorForeignKey) +/// } +/// +/// Foreign keys are always defined from the table that contains the columns at +/// the origin of the foreign key. Person's symmetric HasMany associations reuse +/// Book's foreign keys: +/// +/// struct Person: TableRecord { +/// static let writtenBooks = hasMany(Book.self, using: Book.authorForeignKey) +/// static let translatedBooks = hasMany(Book.self, using: Book.translatorForeignKey) +/// } +/// +/// Foreign keys can also be defined from query interface columns: +/// +/// struct Book: TableRecord { +/// enum Columns: String, ColumnExpression { +/// case id, title, authorId, translatorId +/// } +/// +/// static let authorForeignKey = ForeignKey([Columns.authorId])) +/// static let translatorForeignKey = ForeignKey([Columns.translatorId])) +/// } +/// +/// When the destination table of a foreign key does not define any primary key, +/// you need to provide the full definition of a foreign key: +/// +/// struct Book: TableRecord { +/// static let authorForeignKey = ForeignKey(["authorId"], to: ["id"])) +/// static let author = belongsTo(Person.self, using: authorForeignKey) +/// } public struct ForeignKey { let originColumns: [String] let destinationColumns: [String]?