Skip to content

error occurred while decoding column 43: unexpected null; try decoding as an Option - But struct field *IS* already Option<string> #3336

@reasv

Description

@reasv

Bug Description

I'm running into what I believe must be a bug in SQLx.

I get this error when using query_as! with my struct:

error occurred while decoding column 43: unexpected null; try decoding as an `Option`

In order to identify which column was causing the problem, I replaced a bunch of columns in the select statement with static values, ensuring they would not be null, like this

SELECT
table.column as column_name

replaced with this

SELECT
'value' as column_name

Which worked. I identified the column, which indeed is NULL as a result of a LEFT JOIN in the query (there are several chained LEFT JOIN).

However, the field in the struct is already Option<String>, so SQLx should have no problem populating it with None.

This is a bug and clearly not intended behaviour. I have no idea what kind of edge case triggered this issue, but the query contains several successive LEFT JOIN clauses where columns from the previous LEFT JOIN are used in the ON clause for the next LEFT JOIN, fwiw.

Important Note

The error does not happen if I do this:

SELECT
NULL as column_name

with the problematic column.
So, despite SQLx claiming that the unexpected null in that field is a problem, it only seems to happen when the null comes as a result of the JOIN. If the NULL is a constant value, it has no issue populating the struct field with None.

Minimal Reproduction

https://github.com/reasv/mitsuba/blob/master/src/db.rs#L667C1-L668C1
The columns causing the issue are all three of file_sha256, mitsuba_file_hidden and thumbnail_sha256

Info

  • SQLx version: 0.7.4
  • SQLx features enabled: [ "postgres", "macros", "migrate", "runtime-tokio"]
  • Database server and version: Postgres, from docker container postgres:13
  • Operating system: Windows 11 (client) / Docker on Linux (Postgres)
  • rustc --version: rustc 1.79.0 (129f3b996 2024-06-10)

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions