# Odd error when querying on a unique index

**URL:** <https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664>\
**Category:** GRDB\
**Created:** [September 26, 2020, 6:27pm UTC](https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664 "2020-09-26T18:27:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![remcopoelstra](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@remcopoelstra](https://forums.swift.org/u/remcopoelstra)\
**Post date:** [September 26, 2020, 6:27pm UTC](https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664/1 "2020-09-26T18:27:58Z")

</div>

Hi,

I'm querying the database on a unique index:

```swift
extension DerivableRequest where RowDecoder == Aperture {
    func identicalAperture(as aperture: Aperture) -> Self {
        filter(key: ["code": aperture.code,
                     <a whole list>
                     "flashText": aperture.flashText])
    }
}

```

I get the following error when GRDB tries to make it into a query: `Fatal error: could not convert database value NULL to String (column: `name`, column index: 2, row: [seqno:0 cid:-2 name:NULL])`

All properties of the type are optionals, and many of them are in fact `nil` during the query.

The unique index is created as follows:

```swift
try db.execute(sql: "CREATE UNIQUE INDEX \"uniqueAperture\" ON \"aperture\"
    (IFNULL(code,255) AS code, <a whole list of IFNULL()>,
    IFNULL(flashText,255))")

```

I could not find a GRDB way of creating this unique index and I have a feeling that the `IFNULL()` calls are the problem here.

How can I make this query work?

Thanks in advance.

Kind regards,

Remco Poelstra

---

<div class="post-metadata">

**Author:** ![remcopoelstra](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@remcopoelstra](https://forums.swift.org/u/remcopoelstra)\
**Post date:** [September 26, 2020, 6:54pm UTC](https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664/2 "2020-09-26T18:54:25Z")

</div>

I also get the same error when I construct the query from a bunch of `.filter()` calls, one for each column.

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 26, 2020, 8:25pm UTC](https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664/3 "2020-09-26T20:25:44Z")

</div>

Hello! I think you're facing [Unique Index with function raises Fatal Error · Issue #840 · groue/GRDB.swift · GitHub](https://github.com/groue/GRDB.swift/issues/840)

The fix will ship very soon..

---

<div class="post-metadata">

**Author:** ![remcopoelstra](https://avatars.discourse-cdn.com/v4/letter/r/4bbf92/32.png) [@remcopoelstra](https://forums.swift.org/u/remcopoelstra)\
**Post date:** [September 27, 2020, 7:13am UTC](https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664/4 "2020-09-27T07:13:21Z")

</div>

I'm sorry, I forgot to check the GitHub issues. Again....  
Thanks for fixing this quickly!

---

<div class="post-metadata">

**Author:** ![gwendal.roue](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/gwendal.roue/32/13992_2.png) [@gwendal.roue](https://forums.swift.org/u/gwendal.roue)\
**Post date:** [September 27, 2020, 2:16pm UTC](https://forums.swift.org/t/odd-error-when-querying-on-a-unique-index/40664/5 "2020-09-27T14:16:35Z")

</div>

Fixed in version 5.0.1
