Skip to content

mysql _zero_ TIMESTAMP causes query_as! to fail at runtime #4283

@gregofi

Description

@gregofi

I have found these related issues/pull requests

I searched by TIMESTAMP and UnexpectedNull, did not found anything related.

Description

mysql TIMESTAMP, when defined as

CREATE TABLE events (
    id BIGINT UNSIGNED AUTO_INCREMENT PRIMARY KEY,
    created_at TIMESTAMP,
    updated_at TIMESTAMP
);

is by default not nullable.

> show create table events;
...
  `created_at` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),
  `updated_at` timestamp NOT NULL DEFAULT '0000-00-00 00:00:00',
...

It can contain a "zero" value.
When so, the query_as! fails with UnexpectedNullError.

I think this is due to "zero" value semantically having similar meaning to "missing" (= NULL).
SQLX looks at the type received from the database when generating the query by the macro. The database says its not nullable.
However, when doing insert like this:

INSERT INTO events VALUES ();

it will have the updated_at value equal to 0000-00-00 00:00:00 (= 0).

I am not really profficient with sqlx codebase, but it may be due to this guy returning it as None, and this transforming it into an error.

I am however not sure what this should return, if anything. Changing the return type is probably a no go?
Always considering the TIMESTAMP as Option<...> would work, but I think (not sure) the behavior with the "zero" value can be turned off, so it doesn't always make sense.

I think at least adding a small note to sqlx-mysql/types/mod.rs would help.

Reproduction steps

https://github.com/gregofi/sqlx-mysql-timestamp/

  • Run docker compose up
  • Run cargo run, should panic at runtime.

SQLx version

0.9

Enabled SQLx features

mysql, chrono, macros, runtime-tokio

Database server and version

mariadb 10.5

Operating system

Linux

Rust version

rustc 1.95.0 (59807616e 2026-04-14)

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