-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Connection::run(query: Insert) failed to unwrap lastInsertRowid with OnConflict.Ignore #235
Comments
https://www.sqlite.org/c3ref/last_insert_rowid.html The sqlite3_last_insert_rowid(D) interface returns the rowid of the most recent successful INSERT into a rowid table [...] An INSERT that fails due to a constraint violation is not a successful INSERT and does not change the value returned by this routine. Thus [...], INSERT OR IGNORE, [...] make no changes to the return value of this routine when their insertion fails. |
This method is the culprit, public func run(query: Insert) throws -> Int64 {
let expression = query.expression
return try sync {
try self.run(expression.template, expression.bindings)
return self.lastInsertRowid!
}
} Temporary fix: public func run(query: Insert) throws -> Int64 {
let expression = query.expression
return try sync {
try self.run(expression.template, expression.bindings)
return (self.lastInsertRowid ?? -1)!
}
} |
Also here is alternative fix, but it was rejected. |
I tried that fix, but it does not build for me. |
With my fix you have to check return value on nil. With 'if let' pattern, for example. |
True, also I'm not sure how Stephen feels about returning -1 for failed Insert statements. We shall see. |
Since a row id should be unique number with Ignore, any column with -1 wouldn't fix a problem. I think insert methods should just do db.run without lastInsertRowid. |
@stephencelis How about
|
Fixed in 75394da |
branch: swift-2
commit: 8bd27bc
Model
Insert code
This insert code will fail here if model with the same Id already exists.
The text was updated successfully, but these errors were encountered: