Join GitHub today
GitHub is home to over 28 million developers working together to host and review code, manage projects, and build software together.
Sign upCrash with special timestamp value in mysql (`0000-00-00 00:00:00`) through chrono #1130
Comments
killercup
added
bug
good first issue
labels
Aug 25, 2017
This comment has been minimized.
|
Very good catch! Looks like an easy fix, luckily. From the stack trace: In this code diesel/diesel/src/mysql/types/date_and_time.rs Lines 67 to 71 in 1f8118d we are calling chrono's from_ymd but could just as well call the Same for |
This comment has been minimized.
|
Hi there! I'd like to do this one. |
This comment has been minimized.
|
@alexeyzab great! It's yours :) If you need any help, feel free to ask here or on https://gitter.im/diesel-rs/diesel! |
added a commit
to alexeyzab/diesel
that referenced
this issue
Sep 1, 2017
killercup
added
the
assigned
label
Sep 1, 2017
added a commit
to alexeyzab/diesel
that referenced
this issue
Sep 2, 2017
added a commit
to alexeyzab/diesel
that referenced
this issue
Sep 2, 2017
This comment has been minimized.
|
Fixed by #1137 |
sgrif
closed this
Sep 5, 2017
This comment has been minimized.
konstin
commented
Sep 5, 2017
|
Thanks for fix! Is it right that the current solution still means that the query will fail by returning an |
This comment has been minimized.
|
There's nothing else we can do here. |
This comment has been minimized.
|
Or option C: Use PG. |
This comment has been minimized.
konstin
commented
Sep 5, 2017
|
Thanks for the detailed answer, I see the point now. Using PG isn't an option here as I use data from a different program that is tied to mysql. |
This comment has been minimized.
|
The PG answer was a joke. |
This comment has been minimized.
konstin
commented
Sep 5, 2017
|
I got that it was joke, but sometimes jokes become solutions (though maybe not the one you want), and I've got nothing against giving PG a shot |
This comment has been minimized.
frol
commented
Jun 16, 2018
|
This is my take on getting this weird MySQL 0000-00-00 date handled as I have created a custom field use chrono;
use mysqlclient_sys;
use diesel::mysql::Mysql;
use diesel::sql_types::Date;
use diesel::deserialize::{self, FromSql};
#[derive(Debug, FromSqlRow)]
pub struct MysqlNaiveDate(Option<chrono::NaiveDate>);
impl FromSql<Date, Mysql> for MysqlNaiveDate {
fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
let mysql_time = <mysqlclient_sys::MYSQL_TIME as FromSql<Date, Mysql>>::from_sql(bytes)?;
Ok(MysqlNaiveDate(
if mysql_time.day == 0 && mysql_time.month == 0 && mysql_time.year == 0 {
None
} else {
Some(
chrono::NaiveDate::from_ymd_opt(
mysql_time.year as i32,
mysql_time.month as u32,
mysql_time.day as u32,
).ok_or_else(|| format!("Unable to convert {:?} to chrono", mysql_time))?
)
}
))
}
}and use it like this: #[derive(Debug, Queryable)]
pub struct User {
pub user_id: u32,
pub email: String,
pub birthday: MysqlNaiveDate,
}Here is the table! {
users (user_id) {
user_id -> Unsigned<Integer>,
email -> Varchar,
birthday -> Date,
}
} |
konstin commentedAug 25, 2017
Setup
Versions
Feature Flags
["mysql", "chrono", "large-tables"]["mysql"]Problem Description
When trying to load a value such as
0000-00-00 00:00:00into a NaiveDateTime, there is panic inside chrono, which is called by diesel.Steps to reproduce
0000-00-00 00:00:00NaiveDateTimefield.thread '<unnamed>' panicked at 'invalid or out-of-range date', /checkout/src/libcore/option.rs:819:4Full backtrace
This crash happened trying to read a table with multiple columns, therefore the bigger generic. I'm still sure that the problem is the
0000-00-00 00:00:00as the crash doesn't occur after removing them.Checklist