diff --git a/GRDB/RowModel.swift b/GRDB/RowModel.swift index aef59a58d..221b2dd54 100644 --- a/GRDB/RowModel.swift +++ b/GRDB/RowModel.swift @@ -283,7 +283,25 @@ public class RowModel { fatalError("\(self.dynamicType).storedDatabaseDictionary must return the value for the primary key `(rowIDColumn)`") } if rowID == nil { - setDatabaseValue(DatabaseValue.Integer(changes.insertedRowID!), forColumn: rowIDColumn) + // IMPLEMENTATION NOTE: + // + // We update the ID with updateFromRow(), and not + // setDatabaseValue(_:forColumn:). Rationale: + // + // 1. If subclass updates its ID in setDatabaseValue(), then the + // default updateFromRow() runs, which calls + // setDatabaseValue(), and updates the ID. + // + // 2. If subclass overrides updateFromRow() and updates its ID + // in setDatabaseValue(), then the subclasses calls super + // from updateFromRow() (as it is required to do), which in + // turns call setDatabaseValue(), and updates the ID. + // + // 3. If subclass overrides updateFromRow() and updates its ID + // in updateFromRow(), not in setDatabaseValue(), which it is + // allowed to do, then using setDatabaseValue() would not + // update the ID. + updateFromRow(Row(dictionary: [rowIDColumn: changes.insertedRowID])) } } diff --git a/README.md b/README.md index 54f87ceff..d9f20f170 100644 --- a/README.md +++ b/README.md @@ -1002,7 +1002,7 @@ try dbQueue.inTransaction { db in } ``` -Models that declare a `RowID` primary key have their id automatically set after successful insertion (with the `setDatabaseValue(_:forColumn:)` method). +Models that declare a `RowID` primary key have their id automatically set after successful insertion. Other primary keys (single or multiple columns) are not managed by GRDB: you have to manage them yourself. You can for example override the `insert` primitive method, and make sure your primary key is set before calling `super.insert`.