From b69dfb17095b6fa6431d28067cefb6adbbadd616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 27 Mar 2021 21:15:18 +0100 Subject: [PATCH] Document the type-safe extensions to Identifiable records --- Documentation/AssociationsBasics.md | 4 +- .../GoodPracticesForDesigningRecordTypes.md | 34 +-- README.md | 204 ++++++++---------- 3 files changed, 110 insertions(+), 132 deletions(-) diff --git a/Documentation/AssociationsBasics.md b/Documentation/AssociationsBasics.md index 1456abede..e5fca74be 100644 --- a/Documentation/AssociationsBasics.md +++ b/Documentation/AssociationsBasics.md @@ -89,7 +89,7 @@ struct BookInfo { let books = try Book.fetchAll(db) let bookInfos = books.map { book -> BookInfo in - let author = try Author.fetchOne(db, key: book.authorId) + let author = try Author.fetchOne(db, id: book.authorId) return BookInfo(book: book, author: author) } ``` @@ -1068,7 +1068,7 @@ let bookInfos: [BookInfo] = try BookInfo.fetchAll(db, request) **You can filter associated records.** -The `filter(_:)`, `filter(key:)` and `filter(keys:)` methods, that you already know for [filtering simple requests](../README.md#requests), can filter associated records as well: +The `filter(_:)`, `filter(id:)`, `filter(ids:)`, `filter(key:)` and `filter(keys:)` methods, that you already know for [filtering simple requests](../README.md#requests), can filter associated records as well: ```swift // SELECT book.* diff --git a/Documentation/GoodPracticesForDesigningRecordTypes.md b/Documentation/GoodPracticesForDesigningRecordTypes.md index a2d0dd082..142591116 100644 --- a/Documentation/GoodPracticesForDesigningRecordTypes.md +++ b/Documentation/GoodPracticesForDesigningRecordTypes.md @@ -98,13 +98,13 @@ Define one record type per database table, and make it adopt a [PersistableRecor In this sample code, we'll use Codable structs, but there are [other ways](../README.md#examples-of-record-definitions) to define records. ```swift -struct Author: Codable { +struct Author: Codable, Identifiable { var id: Int64? var name: String var country: String? } -struct Book: Codable { +struct Book: Codable, Identifiable { var id: Int64? var authorId: Int64 var title: String @@ -140,6 +140,15 @@ That's it. The `Author` type can read and write in the `author` database table. > print(author.id!) // Guaranteed non-nil id > } > ``` +< +> :bulb: **Tip**: When the database table has a single-column primary key, have your record type adopt the standard [Identifiable] protocol. This allows GRDB to define type-safe id-related methods: +> +> ```swift +> let authorID: Int64 = ... +> let author: Author? = try dbQueue.read { db in +> try Author.fetchOne(db, id: id) +> } +> ``` Now that `Author` and `Book` can read and write in their own database tables, they are responsible for it. Make sure each record type deals with one database table, and only one database table! @@ -449,7 +458,7 @@ Granted with primitive and derived record types, your application will load the let authorId = 123 let authorInfo: AuthorInfo? = try dbQueue.read { db in let request = Author - .filter(key: authorId) + .filter(id: authorId) .including(all: Author.books) return try AuthorInfo.fetchOne(db, request) } @@ -465,7 +474,7 @@ Granted with primitive and derived record types, your application will load the let bookId = 123 let bookInfo: BookInfo? = try dbQueue.read { db in let request = Book - .filter(key: bookId) + .filter(id: bookId) .including(required: Book.author) return try BookInfo.fetchOne(db, request) } @@ -502,7 +511,7 @@ Those properties provide an alternative way to feed our application: } let authorId = 123 let authorInfo: AuthorInfo? = try dbQueue.read { db in - guard let author = try Author.fetchOne(db, key: authorId) else { + guard let author = try Author.fetchOne(db, id: authorId) else { return nil } let books = try author.books.fetchAll(db) @@ -521,7 +530,7 @@ Those properties provide an alternative way to feed our application: } let bookId = 123 let bookInfo: BookInfo? = try dbQueue.read { db in - guard let book = try Book.fetchOne(db, key: bookId) else { + guard let book = try Book.fetchOne(db, id: bookId) else { return nil } guard let author = try book.author.fetchOne(db) else { @@ -563,7 +572,7 @@ class NaiveLibraryManager { func author(id: Int64) -> Author? { do { return try dbQueue.read { db in - try Author.fetchOne(db, key: id) + try Author.fetchOne(db, id: id) } } catch { return nil @@ -573,7 +582,7 @@ class NaiveLibraryManager { func book(id: Int64) -> Book? { do { return try dbQueue.read { db in - try Book.fetchOne(db, key: id) + try Book.fetchOne(db, id: id) } } catch { return nil @@ -622,13 +631,13 @@ class ImprovedLibraryManager { func author(id: Int64) throws -> Author? { try dbQueue.read { db in - try Author.fetchOne(db, key: id) + try Author.fetchOne(db, id: id) } } func book(id: Int64) throws -> Book? { try dbQueue.read { db in - try Book.fetchOne(db, key: id) + try Book.fetchOne(db, id: id) } } @@ -702,7 +711,7 @@ extension LibraryManager { func bookInfo(bookId: Int64) throws -> BookInfo? { try dbQueue.read { db in - guard let book = try Book.fetchOne(db, key: bookId) else { + guard let book = try Book.fetchOne(db, id: bookId) else { return nil } guard let author = try book.author.fetchOne(db) else { @@ -722,7 +731,7 @@ extension LibraryManager { func authorInfo(authorId: Int64) throws -> AuthorInfo? { try dbQueue.read { db in - guard let author = try Author.fetchOne(db, key: authorId) else { + guard let author = try Author.fetchOne(db, id: authorId) else { return nil } let books = try author.books.fetchAll(db) @@ -820,3 +829,4 @@ Instead, have a look at [Database Observation]: [CodingKeys]: https://developer.apple.com/documentation/foundation/archives_and_serialization/encoding_and_decoding_custom_types [Combine Support]: Combine.md [Value]: ../README.md#values +[Identifiable]: https://developer.apple.com/documentation/swift/identifiable diff --git a/README.md b/README.md index e7b2c1afd..45fd7b414 100644 --- a/README.md +++ b/README.md @@ -243,7 +243,7 @@ See [Records](#records) ```swift try dbQueue.read { db in // Place? - let paris = try Place.fetchOne(db, key: 1) + let paris = try Place.fetchOne(db, id: 1) // Place? let berlin = try Place.filter(Column("title") == "Berlin").fetchOne(db) @@ -2280,7 +2280,7 @@ Records ```swift try dbQueue.write { db in - if var place = try Place.fetchOne(db, key: 1) { + if var place = try Place.fetchOne(db, id: 1) { place.isFavorite = true try place.update(db) } @@ -2352,7 +2352,7 @@ let bestPlayers = try Player // [Player] .limit(10) .fetchAll(db) -let spain = try Country.fetchOne(db, key: "ES") // Country? +let spain = try Country.fetchOne(db, id: "ES") // Country? ``` :point_right: Fetching from raw SQL is available for subclasses of the [Record](#record-class) class, and types that adopt the [FetchableRecord] protocol. @@ -2365,7 +2365,7 @@ let spain = try Country.fetchOne(db, key: "ES") // Country? To update a record in the database, call the `update` method: ```swift -if let player = try Player.fetchOne(db, key: 1) { +if let player = try Player.fetchOne(db, id: 1) { player.score = 1000 try player.update(db) } @@ -2374,7 +2374,7 @@ if let player = try Player.fetchOne(db, key: 1) { It is possible to [avoid useless updates](#record-comparison): ```swift -if var player = try Player.fetchOne(db, key: 1) { +if var player = try Player.fetchOne(db, id: 1) { // does not hit the database if score has not changed try player.updateChanges(db) { $0.score = 1000 @@ -2399,12 +2399,18 @@ try Player To delete a record in the database, call the `delete` method: ```swift -if let player = try Player.fetchOne(db, key: 1) { - try player.delete(db) -} +let player: Player = ... +try player.delete(db) ``` -You can also delete by primary key, or any unique index: +Record types that conform to the standard [Identifiable] protocol can use the type-safe methods `deleteOne(_:id:)` and `deleteAll(_:ids:)`: + +```swift +try Player.deleteOne(db, id: 1) +try Country.deleteAll(db, ids: ["FR", "US"]) +``` + +All types can use `deleteOne(_:key:)` and `deleteAll(_:keys:)` that apply conditions on primary keys and unique keys: ```swift try Player.deleteOne(db, key: 1) @@ -2486,7 +2492,7 @@ Details follow: struct Place: TableRecord, FetchableRecord { ... } try dbQueue.read { db in let places = try Place.order(Column("title")).fetchAll(db) - let paris = try Place.fetchOne(key: 1) + let paris = try Place.fetchOne(id: 1) } ``` @@ -2495,7 +2501,7 @@ Details follow: ```swift struct Place : PersistableRecord { ... } try dbQueue.write { db in - try Place.delete(db, key: 1) + try Place.delete(db, id: 1) try Place(...).insert(db) } ``` @@ -2636,31 +2642,7 @@ When a type adopts both TableRecord and [FetchableRecord](#fetchablerecord-proto let paris = try Place.filter(nameColumn == "Paris").fetchOne(db) ``` -TableRecord can also fetch records by primary key: - -```swift -try Player.fetchOne(db, key: 1) // Player? -try Player.fetchAll(db, keys: [1, 2, 3]) // [Player] -try Player.fetchSet(db, keys: [1, 2, 3]) // Set - -try Country.fetchOne(db, key: "FR") // Country? -try Country.fetchAll(db, keys: ["FR", "US"]) // [Country] -try Country.fetchSet(db, keys: ["FR", "US"]) // Set -``` - -When the table has no explicit primary key, GRDB uses the [hidden "rowid" column](#the-implicit-rowid-primary-key): - -```swift -// SELECT * FROM document WHERE rowid = 1 -try Document.fetchOne(db, key: 1) // Document? -``` - -For multiple-column primary keys and unique keys defined by unique indexes, provide a dictionary: - -```swift -// SELECT * FROM citizenship WHERE citizenId = 1 AND countryCode = 'FR' -try Citizenship.fetchOne(db, key: ["citizenId": 1, "countryCode": "FR"]) // Citizenship? -``` +TableRecord can also fetch records by primary key and unique keys: see [Fetching by Key](#fetching-by-key). ## PersistableRecord Protocol @@ -2782,7 +2764,9 @@ try place.exists(db) // Type methods try Place.updateAll(db, ...) // UPDATE try Place.deleteAll(db) // DELETE +try Place.deleteAll(db, ids:...) // DELETE try Place.deleteAll(db, keys:...) // DELETE +try Place.deleteOne(db, id:...) // DELETE try Place.deleteOne(db, key:...) // DELETE ``` @@ -2982,11 +2966,11 @@ try dbQueue.write { db in // BAD: performs a blob-based query, fails to find the inserted player _ = try Player.filter(Column("uuid") == uuid).fetchOne(db) - _ = try Player.filter(key: uuid).fetchOne(db) + _ = try Player.filter(id: uuid).fetchOne(db) // GOOD: performs a string-based query, finds the inserted player _ = try Player.filter(Column("uuid") == uuid.uuidString).fetchOne(db) - _ = try Player.filter(key: uuid.uuidString).fetchOne(db) + _ = try Player.filter(id: uuid.uuidString).fetchOne(db) } ``` @@ -3167,7 +3151,7 @@ The `updateChanges` methods perform a database update of the changed columns onl This method lets you compare two records: ```swift - if let oldPlayer = try Player.fetchOne(db, key: 42) { + if let oldPlayer = try Player.fetchOne(db, id: 42) { var newPlayer = oldPlayer newPlayer.score = 100 if try newPlayer.updateChanges(db, from: oldPlayer) { @@ -3183,7 +3167,7 @@ The `updateChanges` methods perform a database update of the changed columns onl This method lets you update a record in place: ```swift - if var player = try Player.fetchOne(db, key: 42) { + if var player = try Player.fetchOne(db, id: 42) { let modified = try player.updateChanges(db) { $0.score = 100 } @@ -3201,7 +3185,7 @@ The `updateChanges` methods perform a database update of the changed columns onl ```swift // Record class only - if let player = try Player.fetchOne(db, key: 42) { + if let player = try Player.fetchOne(db, id: 42) { player.score = 100 if try player.updateChanges(db) { print("player was modified, and updated in the database") @@ -3401,10 +3385,10 @@ Some GRDB methods will automatically use this hidden column when a table has no ```swift // SELECT * FROM event WHERE rowid = 1 -let event = try Event.fetchOne(db, key: 1) +let event = try Event.fetchOne(db, id: 1) // DELETE FROM book WHERE rowid = 1 -try Book.deleteOne(db, key: 1) +try Book.deleteOne(db, id: 1) ``` @@ -3793,8 +3777,10 @@ This is the list of record methods, along with their required protocols. The [Re | **Delete Records** | | | | `record.delete(db)` | [PersistableRecord] | | | `Type.deleteOne(db, key:...)` | [PersistableRecord] | ¹ | +| `Type.deleteOne(db, id:...)` | [PersistableRecord] & [Identifiable] | ¹ | | `Type.deleteAll(db)` | [PersistableRecord] | | | `Type.deleteAll(db, keys:...)` | [PersistableRecord] | ¹ | +| `Type.deleteAll(db, ids:...)` | [PersistableRecord] & [Identifiable] | ¹ | | `Type.filter(...).deleteAll(db)` | [PersistableRecord] | ² | | **Check Record Existence** | | | | `record.exists(db)` | [PersistableRecord] | | @@ -3806,24 +3792,28 @@ This is the list of record methods, along with their required protocols. The [Re | **Fetch Record [Cursors](#cursors)** | | | | `Type.fetchCursor(db)` | [FetchableRecord] & [TableRecord] | | | `Type.fetchCursor(db, keys:...)` | [FetchableRecord] & [TableRecord] | ¹ | +| `Type.fetchCursor(db, ids:...)` | [FetchableRecord] & [TableRecord] & [Identifiable] | ¹ | | `Type.fetchCursor(db, sql: sql)` | [FetchableRecord] | ³ | | `Type.fetchCursor(statement)` | [FetchableRecord] | ⁴ | | `Type.filter(...).fetchCursor(db)` | [FetchableRecord] & [TableRecord] | ² | | **Fetch Record Arrays** | | | | `Type.fetchAll(db)` | [FetchableRecord] & [TableRecord] | | | `Type.fetchAll(db, keys:...)` | [FetchableRecord] & [TableRecord] | ¹ | +| `Type.fetchAll(db, ids:...)` | [FetchableRecord] & [TableRecord] & [Identifiable] | ¹ | | `Type.fetchAll(db, sql: sql)` | [FetchableRecord] | ³ | | `Type.fetchAll(statement)` | [FetchableRecord] | ⁴ | | `Type.filter(...).fetchAll(db)` | [FetchableRecord] & [TableRecord] | ² | | **Fetch Record Sets** | | | | `Type.fetchSet(db)` | [FetchableRecord] & [TableRecord] | | | `Type.fetchSet(db, keys:...)` | [FetchableRecord] & [TableRecord] | ¹ | +| `Type.fetchSet(db, ids:...)` | [FetchableRecord] & [TableRecord] & [Identifiable] | ¹ | | `Type.fetchSet(db, sql: sql)` | [FetchableRecord] | ³ | | `Type.fetchSet(statement)` | [FetchableRecord] | ⁴ | | `Type.filter(...).fetchSet(db)` | [FetchableRecord] & [TableRecord] | ² | | **Fetch Individual Records** | | | | `Type.fetchOne(db)` | [FetchableRecord] & [TableRecord] | | | `Type.fetchOne(db, key:...)` | [FetchableRecord] & [TableRecord] | ¹ | +| `Type.fetchOne(db, id:...)` | [FetchableRecord] & [TableRecord] & [Identifiable] | ¹ | | `Type.fetchOne(db, sql: sql)` | [FetchableRecord] | ³ | | `Type.fetchOne(statement)` | [FetchableRecord] | ⁴ | | `Type.filter(...).fetchOne(db)` | [FetchableRecord] & [TableRecord] | ² | @@ -3874,7 +3864,7 @@ This is the list of record methods, along with their required protocols. The [Re ¹ All unique keys are supported: primary keys (single-column, composite, [implicit RowID](#the-implicit-rowid-primary-key)) and unique indexes: ```swift -try Player.fetchOne(db, key: 1) // Player? +try Player.fetchOne(db, id: 1) // Player? try Player.fetchOne(db, key: ["email": "arthur@example.com"]) // Player? try Country.fetchAll(db, keys: ["FR", "US"]) // [Country] ``` @@ -4277,6 +4267,18 @@ You can now build requests with the following methods: `all`, `none`, `select`, Player.filter(nameColumn != nil && heightColumn > 1.75) ``` +- `filter(id:)` and `filter(ids:)` are type-safe methods available on record types that conform to the standard [Identifiable] protocol: + + ```swift + // SELECT * FROM player WHERE id = 1 + Player.filter(id: 1) + + // SELECT * FROM country WHERE isoCode IN ('FR', 'US') + Country.filter(ids: ["FR", "US"]) + ``` + + > :point_up: **Note**: `Identifiable` requires the record type to define an `id` property, but the name of the primary key column can be anything. + - `filter(key:)` and `filter(keys:)` apply conditions on primary keys and unique keys: ```swift @@ -4880,67 +4882,40 @@ You can also change the request so that it knows the type it has to fetch: ``` -## Fetching By Key +## Fetching by Key -**Fetching records according to their primary key** is a very common task. It has a shortcut which accepts any single-column primary key: +**Fetching records according to their primary key** is a common task. + +Record types that conform to the standard [Identifiable] protocol can use the type-safe methods `fetchOne(_:id:)`, `fetchAll(_:ids:)` and `fetchSet(_:ids:)`: + +```swift +try Player.fetchOne(db, id: 1) // Player? +try Country.fetchAll(db, ids: ["FR", "US"]) // [Countries] +``` + +All types can use `fetchOne(_:key:)`, `fetchAll(_:keys:)` and `fetchSet(_:keys:)` that apply conditions on primary keys and unique keys: ```swift -// SELECT * FROM player WHERE id = 1 try Player.fetchOne(db, key: 1) // Player? - -// SELECT * FROM player WHERE id IN (1, 2, 3) -try Player.fetchAll(db, keys: [1, 2, 3]) // [Player] - -// SELECT * FROM country WHERE isoCode = 'FR' -try Country.fetchOne(db, key: "FR") // Country? - -// SELECT * FROM country WHERE isoCode IN ('FR', 'US') try Country.fetchAll(db, keys: ["FR", "US"]) // [Country] +try Player.fetchOne(db, key: ["email": "arthur@example.com"]) // Player? +try Citizenship.fetchOne(db, key: ["citizenId": 1, "countryCode": "FR"]) // Citizenship? ``` When the table has no explicit primary key, GRDB uses the [hidden "rowid" column](#the-implicit-rowid-primary-key): ```swift // SELECT * FROM document WHERE rowid = 1 -try Document.fetchOne(db, key: 1) // Document? +try Document.fetchOne(db, id: 1) // Document? ``` -For multiple-column primary keys and unique keys defined by unique indexes, provide a dictionary: +**When you want to build a request and plan to fetch from it later**, use a `filter` method: ```swift -// SELECT * FROM citizenship WHERE citizenId = 1 AND countryCode = 'FR' -try Citizenship.fetchOne(db, key: ["citizenId": 1, "countryCode": "FR"]) // Citizenship? - -// SELECT * FROM player WHERE email = 'arthur@example.com' -try Player.fetchOne(db, key: ["email": "arthur@example.com"]) // Player? -``` - -**When you want to build a request and plan to fetch from it later**, use the `filter(key:)` and `filter(keys:)` methods: - -```swift -// SELECT * FROM player WHERE id = 1 -let request = Player.filter(key: 1) -let player = try request.fetchOne(db) // Player? - -// SELECT * FROM player WHERE id IN (1, 2, 3) -let request = Player.filter(keys: [1, 2, 3]) -let players = try request.fetchAll(db) // [Player] - -// SELECT * FROM country WHERE isoCode = 'FR' -let request = Country.filter(key: "FR") -let country = try request.fetchOne(db) // Country? - -// SELECT * FROM country WHERE isoCode IN ('FR', 'US') -let request = Country.filter(keys: ["FR", "US"]) -let countries = try request.fetchAll(db) // [Country] - -// SELECT * FROM citizenship WHERE citizenId = 1 AND countryCode = 'FR' -let request = Citizenship.filter(key: ["citizenId": 1, "countryCode": "FR"]) -let citizenship = request.fetchOne(db) // Citizenship? - -// SELECT * FROM player WHERE email = 'arthur@example.com' +let request = Player.filter(id: 1) +let request = Country.filter(ids: ["FR", "US"]) let request = Player.filter(key: ["email": "arthur@example.com"]) -let player = try request.fetchOne(db) // Player? +let request = Citizenship.filter(key: ["citizenId": 1, "countryCode": "FR"]) ``` @@ -4998,37 +4973,29 @@ try Player > :point_up: **Note** Deletion methods are only available for records that adopts the [PersistableRecord] protocol. -**Deleting records according to their primary key** is also quite common. It has a shortcut which accepts any single-column primary key: +**Deleting records according to their primary key** is a common task. + +Record types that conform to the standard [Identifiable] protocol can use the type-safe methods `deleteOne(_:id:)` and `deleteAll(_:ids:)`: + +```swift +try Player.deleteOne(db, id: 1) +try Country.deleteAll(db, ids: ["FR", "US"]) +``` + +All types can use `deleteOne(_:key:)` and `deleteAll(_:keys:)` that apply conditions on primary keys and unique keys: ```swift -// DELETE FROM player WHERE id = 1 try Player.deleteOne(db, key: 1) - -// DELETE FROM player WHERE id IN (1, 2, 3) -try Player.deleteAll(db, keys: [1, 2, 3]) - -// DELETE FROM country WHERE isoCode = 'FR' -try Country.deleteOne(db, key: "FR") - -// DELETE FROM country WHERE isoCode IN ('FR', 'US') try Country.deleteAll(db, keys: ["FR", "US"]) +try Player.deleteOne(db, key: ["email": "arthur@example.com"]) +try Citizenship.deleteOne(db, key: ["citizenId": 1, "countryCode": "FR"]) ``` When the table has no explicit primary key, GRDB uses the [hidden "rowid" column](#the-implicit-rowid-primary-key): ```swift // DELETE FROM document WHERE rowid = 1 -try Document.deleteOne(db, key: 1) -``` - -For multiple-column primary keys and unique keys defined by unique indexes, provide a dictionary: - -```swift -// DELETE FROM citizenship WHERE citizenId = 1 AND countryCode = 'FR' -try Citizenship.deleteOne(db, key: ["citizenId": 1, "countryCode": "FR"]) - -// DELETE FROM player WHERE email = 'arthur@example.com' -Player.deleteOne(db, key: ["email": "arthur@example.com"]) +try Document.deleteOne(db, id: 1) // Document? ``` @@ -5054,9 +5021,9 @@ try Player .limit(10) .updateAll(db, Column("top").set(to: true)) -// UPDATE country SET population = 67848156 WHERE code = 'FR' +// UPDATE country SET population = 67848156 WHERE id = 'FR' try Country - .filter(key: "FR") + .filter(id: "FR") .updateAll(db, Column("population").set(to: 67_848_156)) ``` @@ -5775,7 +5742,7 @@ For example: ```swift // Turn an observation of Player? into an observation of UIImage? let observation = ValueObservation - .tracking { db in try Player.fetchOne(db, key: 42) } + .tracking { db in try Player.fetchOne(db, id: 42) } .map { player in player?.image } ``` @@ -5791,7 +5758,7 @@ For example: ```swift // An observation of distinct Player? let observation = ValueObservation - .tracking { db in try Player.fetchOne(db, key: 42) } + .tracking { db in try Player.fetchOne(db, id: 42) } .removeDuplicates() ``` @@ -5799,7 +5766,7 @@ let observation = ValueObservation ```swift // An observation of distinct Player? -let request = Player.filter(key: 42) +let request = Player.filter(id: 42) let observation = ValueObservation .tracking { db in try Row.fetchOne(db, request) } .removeDuplicates() // Row adopts Equatable @@ -5958,7 +5925,7 @@ When needed, you can help GRDB optimize observations and reduce database content // Tracks the row with id 42 in the 'player' table (only) let observation = ValueObservation.trackingConstantRegion { db -> Player? in - try Player.fetchOne(db, key: 42) + try Player.fetchOne(db, id: 42) } // Tracks the 'score' column in the 'player' table (only) @@ -5982,7 +5949,7 @@ When needed, you can help GRDB optimize observations and reduce database content // Does not always track the same row in the player table. let observation = ValueObservation.tracking { db -> Player? in let pref = try Preference.fetchOne(db) ?? .default - return try Player.fetchOne(db, key: pref.favoritePlayerId) + return try Player.fetchOne(db, id: pref.favoritePlayerId) } // Only tracks the 'user' table if there are some blocked emails. @@ -6769,7 +6736,7 @@ When you use [records](#records) and the [query interface](#the-query-interface) let id = 1 let name = textField.text try dbQueue.write { db in - if var student = try Student.fetchOne(db, key: id) { + if var student = try Student.fetchOne(db, id: id) { student.name = name try student.update(db) } @@ -8312,3 +8279,4 @@ This chapter has been superseded by [ValueObservation] and [DatabaseRegionObserv [Database Observation]: #database-changes-observation [SQLRequest]: http://groue.github.io/GRDB.swift/docs/5.6/Structs/SQLRequest.html [SQL literal]: Documentation/SQLInterpolation.md#sql-literal +[Identifiable]: https://developer.apple.com/documentation/swift/identifiable