Details
-
Suggestion
-
Resolution: Fixed
-
Not Evaluated
-
5.9, 5.10, 5.11
-
None
-
-
ce90248882b73b4b816a8550710279e3e55ab64c
Description
When the user tries to add a entity twice, the application should be able to tell him the error precisely.
Technically, a UNIQUE constraint is used in a SQLite database. When executing a insert query, using QSqlQuery, and it fails, I use query.lastError().nativeErrorCode(). (defined in QSqlError).
The problem is that it returns the code 19, which is SQLITE_CONSTRAINT, defined in sqlite3.h.
This error is to generic (it could be foreign key constraint error, etc..).
SQLite provides the concept of extended result codes.
For the above case , SQLITE_CONSTRAINT_UNIQUE (2067) should be returned.
As described in the SQLite documentation, returning extended result codes by default is disabled for historical compatibility reason.
SQLite provides a function to return extended reult codes by default.
My suggestion is to add connection option ( see QSqlDatabase::setConnectOptions() ): QSQLITE_USE_EXTENDED_RESULT_CODES .
In QSQLiteDriver::open() method, sqlite3_extended_result_codes(d->access, 1) could be called if this option is set. I attached the modified qsql_sqlite.cpp .