# Caqti and unique violation error

**URL:** <https://discuss.ocaml.org/t/caqti-and-unique-violation-error/16934>\
**Category:** Learning\
**Tags:** databases\
**Created:** [July 9, 2025, 11:17am UTC](https://discuss.ocaml.org/t/caqti-and-unique-violation-error/16934 "2025-07-09T11:17:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![cr4zsci](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/cr4zsci/32/5964_2.png) [@cr4zsci](https://discuss.ocaml.org/u/cr4zsci)\
**Post date:** [July 9, 2025, 11:17am UTC](https://discuss.ocaml.org/t/caqti-and-unique-violation-error/16934/1 "2025-07-09T11:17:06Z")

</div>

I have a function that returns ('a, Caqti\_error.t) result and I’m trying to catch unique violation error

```auto
  | Ok _ -> ....
  | Error err -> (
      match err with
      | `Response_failed err -> (
          let cause = `Response_failed err |> Caqti_error.cause in
          match cause with
          | `Integrity_constraint_violation__don't_match ->
              print_string "intercepted!"
          | _ -> ())
      | _ -> ())

```

After launching I see in the console “intercepted!”, Am I doing the type narrowing correctly? Why is the uniqueness error in Response\_failed and not in Request\_failed? Or is this a feature of the sqlite3 driver?

---

<div class="post-metadata">

**Author:** ![paurkedal](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/paurkedal/32/586_2.png) [@paurkedal](https://discuss.ocaml.org/u/paurkedal)\
**Post date:** [December 18, 2025, 8:40am UTC](https://discuss.ocaml.org/t/caqti-and-unique-violation-error/16934/2 "2025-12-18T08:40:54Z")

</div>

The detailed error causes are only supported for `sqlite3.5.2.0` or later, since earlier versions of the bindings don’t support the extended errors.

I opened [an issue about the error classification](https://github.com/paurkedal/ocaml-caqti/issues/132), thanks!

I can see it’s a long time since you posted this, I hope my reply is still useful. I recommend using the issue tracker if you find things not working according to expectations.

---

<div class="post-metadata">

**Author:** ![paurkedal](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/paurkedal/32/586_2.png) [@paurkedal](https://discuss.ocaml.org/u/paurkedal)\
**Post date:** [December 19, 2025, 8:20am UTC](https://discuss.ocaml.org/t/caqti-and-unique-violation-error/16934/3 "2025-12-19T08:20:20Z")

</div>

I resolved the issue with the classification of constraint violation errors by switching them to ` `Request_failed`. I think it’s fair to treat this as a bug, since the original classification was inconsistent with the other drivers and the definition of response-failed in the documentation, but there is a risk that sqlite3-only code relies on the past behaviour.

My advice for users of the sqlite3 driver who needs to catch constraint violations is to match it with

```ocaml
| Error (`Request_failed _ | `Response_failed _ as err) ->
    let cause = Caqti_error.cause err in
    ...

```

until you can require `"caqti-driver-sqlite3" {> "2.2.4"}`.
