Skip to content
This repository has been archived by the owner on Apr 25, 2023. It is now read-only.

Commit

Permalink
Merge pull request #81 from prisma/mysql/handle-more-null-constraint-…
Browse files Browse the repository at this point in the history
…errors

Handle error code 1048 on MySQL
  • Loading branch information
tomhoule authored Feb 13, 2020
2 parents da764ce + bf5453a commit add363a
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 15 deletions.
54 changes: 40 additions & 14 deletions src/connector/mysql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ mod tests {
use url::Url;

lazy_static! {
static ref CONN_STR: String = env::var("TEST_MYSQL").unwrap();
static ref CONN_STR: String = env::var("TEST_MYSQL").expect("TEST_MYSQL env var");
}

#[test]
Expand Down Expand Up @@ -464,22 +464,48 @@ VALUES (1, 'Joe', 27, 20000.00 );
.await
.unwrap();

let res = conn
.query_raw("INSERT INTO test_null_constraint_violation () VALUES ()", &[])
.await;
// Error code 1364
{
let res = conn
.query_raw("INSERT INTO test_null_constraint_violation () VALUES ()", &[])
.await;

let err = res.unwrap_err();

match err.kind() {
ErrorKind::NullConstraintViolation { constraint } => {
assert_eq!(Some("1364"), err.original_code());
assert_eq!(
Some("Field \'id1\' doesn\'t have a default value"),
err.original_message()
);
assert_eq!(&DatabaseConstraint::Fields(vec![String::from("id1")]), constraint)
}
_ => panic!(err),
}
}

let err = res.unwrap_err();
// Error code 1048
{
conn.query_raw(
"INSERT INTO test_null_constraint_violation (id1, id2) VALUES (50, 55)",
&[],
)
.await
.unwrap();

match err.kind() {
ErrorKind::NullConstraintViolation { constraint } => {
assert_eq!(Some("1364"), err.original_code());
assert_eq!(
Some("Field \'id1\' doesn\'t have a default value"),
err.original_message()
);
assert_eq!(&DatabaseConstraint::Fields(vec![String::from("id1")]), constraint)
let err = conn
.query_raw("UPDATE test_null_constraint_violation SET id2 = NULL", &[])
.await
.unwrap_err();

match err.kind() {
ErrorKind::NullConstraintViolation { constraint } => {
assert_eq!(Some("1048"), err.original_code());
assert_eq!(&DatabaseConstraint::Fields(vec![String::from("id2")]), constraint);
}
_ => panic!("{:?}", err),
}
_ => panic!(err),
}
}
}
2 changes: 1 addition & 1 deletion src/connector/mysql/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl From<my::error::Error> for Error {

builder.build()
}
my::error::Error::Server(ServerError { ref message, code, .. }) if code == 1364 => {
my::error::Error::Server(ServerError { ref message, code, .. }) if code == 1364 || code == 1048 => {
let splitted: Vec<&str> = message.split_whitespace().collect();
let splitted: Vec<&str> = splitted.get(1).map(|s| s.split('\'').collect()).unwrap();

Expand Down

0 comments on commit add363a

Please sign in to comment.