diff --git a/sqlx-core/src/error.rs b/sqlx-core/src/error.rs index 9ad5eff464..94806eb2e0 100644 --- a/sqlx-core/src/error.rs +++ b/sqlx-core/src/error.rs @@ -197,6 +197,8 @@ pub enum ErrorKind { NotNullViolation, /// Check constraint violation. CheckViolation, + /// Exclusion constraint violation. + ExclusionViolation, /// An unmapped error. Other, } diff --git a/sqlx-postgres/src/error.rs b/sqlx-postgres/src/error.rs index db8bcc8a10..7b5a03f2b3 100644 --- a/sqlx-postgres/src/error.rs +++ b/sqlx-postgres/src/error.rs @@ -214,6 +214,7 @@ impl DatabaseError for PgDatabaseError { error_codes::FOREIGN_KEY_VIOLATION => ErrorKind::ForeignKeyViolation, error_codes::NOT_NULL_VIOLATION => ErrorKind::NotNullViolation, error_codes::CHECK_VIOLATION => ErrorKind::CheckViolation, + error_codes::EXCLUSION_VIOLATION => ErrorKind::ExclusionViolation, _ => ErrorKind::Other, } } @@ -239,4 +240,6 @@ pub(crate) mod error_codes { pub const NOT_NULL_VIOLATION: &str = "23502"; /// Caused when a check constraint is violated. pub const CHECK_VIOLATION: &str = "23514"; + /// Caused when a exclude constraint is violated. + pub const EXCLUSION_VIOLATION: &str = "23P01"; } diff --git a/tests/postgres/error.rs b/tests/postgres/error.rs index 32bf814770..23139e857a 100644 --- a/tests/postgres/error.rs +++ b/tests/postgres/error.rs @@ -75,6 +75,28 @@ async fn it_fails_with_check_violation() -> anyhow::Result<()> { Ok(()) } +#[sqlx_macros::test] +async fn it_fails_with_exclude_violation() -> anyhow::Result<()> { + let mut conn = new::().await?; + let mut tx = conn.begin().await?; + + sqlx::query("INSERT INTO circles VALUES (circle('(0,0)'::point, 5.0));") + .execute(&mut *tx) + .await?; + + let res: Result<_, sqlx::Error> = + sqlx::query("INSERT INTO circles VALUES (circle('(0,2.0)'::point, 2.0));") + .execute(&mut *tx) + .await; + let err = res.unwrap_err(); + + let err = err.into_database_error().unwrap(); + + assert_eq!(err.kind(), ErrorKind::ExclusionViolation); + + Ok(()) +} + #[sqlx_macros::test] async fn it_fails_with_begin_failed() -> anyhow::Result<()> { let mut conn = new::().await?; diff --git a/tests/postgres/setup.sql b/tests/postgres/setup.sql index e3b35d3158..4d89062799 100644 --- a/tests/postgres/setup.sql +++ b/tests/postgres/setup.sql @@ -63,3 +63,8 @@ CREATE SCHEMA IF NOT EXISTS foo; CREATE TYPE foo."Foo" as ENUM ('Bar', 'Baz'); CREATE TABLE mytable(f HSTORE); + +CREATE TABLE circles ( + c circle, + EXCLUDE USING gist (c WITH &&) +);