mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-26 04:07:50 +00:00
1 line
9.7 KiB
JSON
1 line
9.7 KiB
JSON
{"seeAlsoSections":[{"title":"Supporting Types","identifiers":["doc:\/\/GRDB\/documentation\/GRDB\/Inflections"],"generated":true}],"schemaVersion":{"major":0,"minor":3,"patch":0},"metadata":{"fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ForeignKey"}],"title":"ForeignKey","roleHeading":"Structure","role":"symbol","symbolKind":"struct","externalID":"s:4GRDB10ForeignKeyV","modules":[{"name":"GRDB"}],"navigatorTitle":[{"kind":"identifier","text":"ForeignKey"}]},"abstract":[{"type":"text","text":"A "},{"type":"codeVoice","code":"ForeignKey"},{"type":"text","text":" defines on which columns an association between two tables"},{"type":"text","text":" "},{"type":"text","text":"is established."}],"sections":[],"identifier":{"url":"doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey","interfaceLanguage":"swift"},"relationshipsSections":[{"identifiers":["doc:\/\/GRDB\/SQ"],"kind":"relationships","title":"Conforms To","type":"conformsTo"}],"hierarchy":{"paths":[["doc:\/\/GRDB\/documentation\/GRDB","doc:\/\/GRDB\/documentation\/GRDB\/QueryInterface","doc:\/\/GRDB\/documentation\/GRDB\/Association"]]},"topicSections":[{"title":"Initializers","identifiers":["doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey\/init(_:to:)-4wjq3","doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey\/init(_:to:)-7jskm"]},{"title":"Default Implementations","identifiers":["doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey\/Equatable-Implementations"],"generated":true}],"variants":[{"paths":["\/documentation\/grdb\/foreignkey"],"traits":[{"interfaceLanguage":"swift"}]}],"kind":"symbol","primaryContentSections":[{"kind":"declarations","declarations":[{"tokens":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ForeignKey"}],"languages":["swift"],"platforms":["macOS"]}]},{"kind":"content","content":[{"anchor":"overview","level":2,"type":"heading","text":"Overview"},{"type":"paragraph","inlineContent":[{"type":"text","text":"You will need a "},{"type":"codeVoice","code":"ForeignKey"},{"type":"text","text":" when you define an "},{"type":"reference","isActive":true,"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/Association"},{"type":"text","text":" between two"},{"type":"text","text":" "},{"type":"text","text":"tables that are not unambiguously related with a single SQLite foreign key."}]},{"type":"paragraph","inlineContent":[{"type":"text","text":"Sometimes the database schema does not define any foreign key between two"},{"type":"text","text":" "},{"type":"text","text":"tables. And sometimes, there are several foreign keys from a table"},{"type":"text","text":" "},{"type":"text","text":"to another:"}]},{"type":"codeListing","syntax":null,"code":["| Table book | | Table person |","| ------------ | | ------------ |","| id | +-->• id |","| authorId •---+ | name |","| translatorId •---+","| title |"]},{"type":"paragraph","inlineContent":[{"type":"text","text":"When this happens, associations can’t be automatically inferred from the"},{"type":"text","text":" "},{"type":"text","text":"database schema. GRDB will complain with a fatal error such as “Ambiguous"},{"type":"text","text":" "},{"type":"text","text":"foreign key from book to person”, or “Could not infer foreign key from book"},{"type":"text","text":" "},{"type":"text","text":"to person”."}]},{"type":"paragraph","inlineContent":[{"type":"text","text":"Your help is needed. You have to instruct which foreign key to use."},{"type":"text","text":" "},{"type":"text","text":"For example:"}]},{"type":"codeListing","syntax":"swift","code":["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,"," key: \"author\","," using: authorForeignKey)"," static let translator = belongsTo("," Person.self,"," key: \"translator\","," using: translatorForeignKey)","}"]},{"type":"paragraph","inlineContent":[{"type":"text","text":"Foreign keys can also be defined from query interface columns:"}]},{"type":"codeListing","syntax":"swift","code":["struct Book: TableRecord {"," enum Columns: String, ColumnExpression {"," case id, title, authorId, translatorId"," }",""," static let authorForeignKey = ForeignKey([Columns.authorId]))"," static let translatorForeignKey = ForeignKey([Columns.translatorId]))","}"]},{"type":"paragraph","inlineContent":[{"type":"text","text":"When the destination table does not define any primary key, you need to"},{"type":"text","text":" "},{"type":"text","text":"provide the destination columns:"}]},{"type":"codeListing","syntax":"swift","code":["struct Book: TableRecord {"," static let authorForeignKey = ForeignKey([\"authorId\"], to: [\"id\"]))"," static let translatorForeignKey = ForeignKey([\"translatorId\"], to: [\"id\"]))","}"]},{"type":"paragraph","inlineContent":[{"type":"text","text":"Foreign keys are always defined from the table that contains the columns at"},{"type":"text","text":" "},{"type":"text","text":"the origin of the foreign key. "},{"type":"codeVoice","code":"Person"},{"type":"text","text":"’s symmetric associations reuse"},{"type":"text","text":" "},{"type":"text","text":"foreign keys of "},{"type":"codeVoice","code":"Book"},{"type":"text","text":":"}]},{"type":"codeListing","syntax":"swift","code":["struct Person: TableRecord {"," static let writtenBooks = hasMany("," Book.self,"," key: \"writtenBooks\","," using: Book.authorForeignKey)"," static let translatedBooks = hasMany("," Book.self,"," key: \"translatedBooks\","," using: Book.translatorForeignKey)","}"]}]}],"references":{"doc://GRDB/documentation/GRDB/ForeignKey/Equatable-Implementations":{"role":"collectionGroup","title":"Equatable Implementations","abstract":[],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey\/Equatable-Implementations","kind":"article","type":"topic","url":"\/documentation\/grdb\/foreignkey\/equatable-implementations"},"doc://GRDB/documentation/GRDB/Inflections":{"role":"symbol","title":"Inflections","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"Inflections"}],"abstract":[{"type":"text","text":"A type that controls GRDB string inflections."}],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/Inflections","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"Inflections"}],"url":"\/documentation\/grdb\/inflections"},"doc://GRDB/documentation/GRDB/ForeignKey/init(_:to:)-4wjq3":{"role":"symbol","title":"init(_:to:)","fragments":[{"kind":"identifier","text":"init"},{"kind":"text","text":"(["},{"kind":"typeIdentifier","text":"ColumnExpression","preciseIdentifier":"s:4GRDB16ColumnExpressionP"},{"kind":"text","text":"], "},{"kind":"externalParam","text":"to"},{"kind":"text","text":": ["},{"kind":"typeIdentifier","text":"ColumnExpression","preciseIdentifier":"s:4GRDB16ColumnExpressionP"},{"kind":"text","text":"]?)"}],"abstract":[],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey\/init(_:to:)-4wjq3","kind":"symbol","type":"topic","url":"\/documentation\/grdb\/foreignkey\/init(_:to:)-4wjq3"},"doc://GRDB/documentation/GRDB/ForeignKey/init(_:to:)-7jskm":{"role":"symbol","title":"init(_:to:)","fragments":[{"kind":"identifier","text":"init"},{"kind":"text","text":"(["},{"kind":"typeIdentifier","text":"String","preciseIdentifier":"s:SS"},{"kind":"text","text":"], "},{"kind":"externalParam","text":"to"},{"kind":"text","text":": ["},{"kind":"typeIdentifier","text":"String","preciseIdentifier":"s:SS"},{"kind":"text","text":"]?)"}],"abstract":[],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey\/init(_:to:)-7jskm","kind":"symbol","type":"topic","url":"\/documentation\/grdb\/foreignkey\/init(_:to:)-7jskm"},"doc://GRDB/documentation/GRDB":{"role":"collection","title":"GRDB","abstract":[{"type":"text","text":"A toolkit for SQLite databases, with a focus on application development"}],"identifier":"doc:\/\/GRDB\/documentation\/GRDB","kind":"symbol","type":"topic","url":"\/documentation\/grdb"},"doc://GRDB/SQ":{"type":"unresolvable","title":"Swift.Equatable","identifier":"doc:\/\/GRDB\/SQ"},"doc://GRDB/documentation/GRDB/Association":{"role":"symbol","title":"Association","fragments":[{"kind":"keyword","text":"protocol"},{"kind":"text","text":" "},{"kind":"identifier","text":"Association"}],"abstract":[{"type":"text","text":"A type that defines a connection between two tables."}],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/Association","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"Association"}],"url":"\/documentation\/grdb\/association"},"doc://GRDB/documentation/GRDB/QueryInterface":{"role":"collectionGroup","title":"Record Types and the Query Interface","abstract":[{"type":"text","text":"Record types and the query interface build SQL queries for you"}],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/QueryInterface","kind":"article","type":"topic","url":"\/documentation\/grdb\/queryinterface"},"doc://GRDB/documentation/GRDB/ForeignKey":{"role":"symbol","title":"ForeignKey","fragments":[{"kind":"keyword","text":"struct"},{"kind":"text","text":" "},{"kind":"identifier","text":"ForeignKey"}],"abstract":[{"type":"text","text":"A "},{"type":"codeVoice","code":"ForeignKey"},{"type":"text","text":" defines on which columns an association between two tables"},{"type":"text","text":" "},{"type":"text","text":"is established."}],"identifier":"doc:\/\/GRDB\/documentation\/GRDB\/ForeignKey","kind":"symbol","type":"topic","navigatorTitle":[{"kind":"identifier","text":"ForeignKey"}],"url":"\/documentation\/grdb\/foreignkey"}}} |