Grdb.swift: Unique Index with function raises Fatal Error

Created on 26 Sep 2020  路  3Comments  路  Source: groue/GRDB.swift

What did you do?

In GRDB 4.14.0, I have an index that effectively treats NULL as a unique value.

try db.execute(sql: """
CREATE UNIQUE INDEX IF NOT EXISTS
'idx_labor_hours' ON 'laborHours' (
    date,
    ifnull(jobGuid, '\(emptyGuid)'),
    ifnull(foremanGuid, '\(emptyGuid)'),
    ifnull(costCodeGuid, '\(emptyGuid)')
) WHERE isDeleted=0
""")

Perhaps I'm misunderstanding, but based on what I read from SQLite's Indexes On Expressions page, this should be valid, since deterministic functions are allowed in the indexed-column portion of the statement.

What did you expect to happen?

In GRDB 4.14.0, this index works as I expected. For example, if a row "2020-09-25, guid A, NULL, NULL" exists in the database, then I cannot create another row "2020-09-25, guid A, NULL, NULL".

What happened instead?

After updating to to GRDB 5.0.0, queries against the table with this index cause a fatal error Fatal error: could not convert database value NULL to String (column: 'name', column index: 2, row: [seqno:1 cid:-2 name:NULL])
in public func indexes(on tableName: String) throws -> [IndexInfo] in Database+Schema.swift (line 204), while attempting to determine a column name on the index.

I'm guessing that because the index uses a function, there is no column name, hence the NULL value. In GRDB 4.14.0, this function does not appear to be called (my breakpoints were not hit; the pod was compiled with no optimizations), which might be how it was working previously? Is there a way to give a name to the function portions (aliasing with AS does not appear to be valid)?

Environment

GRDB flavor(s): GRDB, GRDBCombine when running GRDB 4.14.0 (might not factor in here)
GRDB version: 4.14.0 -> 5.0.0
Installation method: CocoaPods for 4.14.0, Swift Package Manager for 5.0.0
Xcode version: 12.0
Swift version: 5
Platform(s) running GRDB: iOS 14
macOS version running Xcode: 10.15.7

Demo Project

If needed, I can work on getting a demo project put together.

bug

Most helpful comment

Fixed in in v5.0.1!

All 3 comments

Hello @wyattbeavers, thanks for reporting this issue. You did nothing wrong: this is a plain and simple regression which needs to be fixed!

Fixed in in v5.0.1!

Fixed in in v5.0.1!

Thank you!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

yarshure picture yarshure  路  12Comments

sobri909 picture sobri909  路  21Comments

Spacelapp picture Spacelapp  路  23Comments

simme picture simme  路  20Comments

hartbit picture hartbit  路  50Comments