New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Schema generation: Nullable<Timestamp> instead of Nullable<Timestamptz> #1767

Closed
gnunicorn opened this Issue Jun 27, 2018 · 1 comment

Comments

Projects
None yet
2 participants
@gnunicorn

gnunicorn commented Jun 27, 2018

Setup

Versions

  • Rust: nightly:
  • rustc 1.28.0-nightly (01cc982e9 2018-06-24)
  • cargo 1.28.0-nightly (e2348c2db 2018-06-07)
  • Diesel:
    • cli: 1.3.1
    • cargo.toml: 1.3.2
  • Database: Postgresql 10.4-3
  • Operating System: Arch Linux (Kernel 4.16.13-2-ARCH)

Feature Flags

  • diesel:: ["postgres","chrono","serde_json", "r2d2" , "uuid"]

Problem Description

Schema generation detects and writes Nullable<Timestamp>, where it should be Nullable<Timestamptz>.

What are you trying to accomplish?

I am trying to query/update null-able Timestamp columns on postgres.

What is the expected output?

I expect cargo run to work with the diesel-generated schema.

What is the actual output?

when I do cargo run it fails with:

error[E0271]: type mismatch resolving `<diesel::dsl::now as diesel::Expression>::SqlType == diesel::sql_types::Nullable<diesel::sql_types::Timestamp>`
  --> src/handler/activist.rs:40:35
   |
40 |                 .set(last_seen_at.eq(now))
   |                                   ^^ expected struct `diesel::sql_types::Timestamp`, found struct `diesel::sql_types::Nullable`
   |
   = note: expected type `diesel::sql_types::Timestamp`
              found type `diesel::sql_types::Nullable<diesel::sql_types::Timestamp>`

error: aborting due to previous error

For more information about this error, try `rustc --explain E0271`.
error: Could not compile

on my queries.

Steps to reproduce

I have the schema as follows (excerpt) - most important here is the last_seen_at entry:

CREATE EXTENSION pgcrypto;

CREATE TABLE activists (
	uuid          uuid NOT NULL DEFAULT gen_random_uuid(),
	name          varchar(120) NOT NULL,
	friendly_id   varchar(120) NOT NULL,
	created_at    timestamp NOT NULL DEFAULT (now() at time zone 'utc'),
    last_seen_at  timestamp
);

running diesel migrate run, this creates the proper schema in my Postgresql, however it also creates the following schema file entry:

 table! {
    activists (uuid) {
        uuid -> Uuid,
        name -> Varchar,
        friendly_id -> Varchar,
        created_at -> Timestamp,
        last_seen_at -> Nullable<Timestamp>,
    }
}

Fixes

It took me a while to figure out, but I am able to fix this by replacing Timestamp with Timestamptz in the schema file. Then everything works as expected. This works:

 table! {
    activists (uuid) {
        uuid -> Uuid,
        name -> Varchar,
        friendly_id -> Varchar,
        created_at -> Timestamptz,
        last_seen_at -> Nullable<Timestamptz>,
    }
}

Therefore I am assuming this is caused by a fault in the detection/generation.

Unfortunately I have to remember to do that every time I did a diesel migration run (or redo), which makes this a little annoying.

Checklist

  • I have already looked over the issue tracker for similar issues.
  • This issue can be reproduced on Rust's stable channel. (Your issue will be
    closed if this is not the case)

@gnunicorn gnunicorn changed the title from Schemageneration: Nullable<Timestamp> created instead of Nullable<Timestamptz> to Schema generation: Nullable<Timestamp> instead of Nullable<Timestamptz> Jun 27, 2018

@sgrif

This comment has been minimized.

Member

sgrif commented Jun 27, 2018

Schema inference is doing the right thing. Your column is of type timestamp in your migration, not timestamptz or timestamp with time zone. Your issue in your code has nothing to do with this, you need to change now to now.nullable()

@sgrif sgrif closed this Jun 27, 2018

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment