4

I want select the max value using this query (all fields in table are not null):

dc.run(quote {
    query[SchemaInfo]
      .filter(_.subjectName == lift(subject))
      .map(_.version)
      .max
  }).map(_.map(_ + 1).getOrElse(1))

I know, that table may be empty, so i use this: map(_.map(_ + 1).getOrElse(1)).

The problem is that this query produces this error:

SQL NULL read at column 1 (JDBC type null) but mapping is to a non-Option type; use Option here. Note that JDBC column indexing is 1-based. doobie.util.invariant$NonNullableColumnRead: SQL NULL read at column 1 (JDBC type null) but mapping is to a non-Option type; use Option here. Note that JDBC column indexing is 1-based.

How to fix it? Without quill (using pure doobie) the same query working correctly

Ossip
  • 1,046
  • 8
  • 20
Nikita Ryanov
  • 1,520
  • 3
  • 17
  • 34

1 Answers1

7

I know that you probably got the answer already, but I will leave my comment here so that it hopefully help someone in the future.

so the problem is in line query[SchemaInfo] as the record may not exist it should be mapped into query[Option[SchemaInfo]].unique

pezetem
  • 2,503
  • 2
  • 20
  • 38
  • Tricky, since the [BOD](https://tpolecat.github.io/doobie/docs/04-Selecting.html#reading-rows-into-collections) says `.option` "returns an Option, raising an exception if there is more than one row returned" (rather than "unless exactly one row is returned") implying that it does *not* raise an exception if there is fewer than one row returned. – Adam Mackler Mar 13 '23 at 15:19