RowModel.insert() updates the ID with updateFromRow().

This commit is contained in:
Gwendal Roué committed 2015-09-03 14:29:12 +02:00
1 parent b10d5cdffe
commit 6ebc2f441a
2 files changed
+20 -2

No files matched your search

+19 -1
View File
@@ -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]))
}
}
+1 -1
View File
@@ -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`.