Skip to content

Commit b7bdd4e

Browse files
nicoulajtyt2y3
authored andcommitted
fix bug in Condition::add where Condition negation is ignored
1 parent 0b0d7aa commit b7bdd4e

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

src/query/condition.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,24 @@ impl Condition {
4646
///
4747
/// If it's an [`Condition::any`], it will be separated from the others by an `" OR "` in the query. If it's
4848
/// an [`Condition::all`], it will be separated by an `" AND "`.
49+
///
50+
/// ```
51+
/// use sea_query::{tests_cfg::*, *};
52+
///
53+
/// let statement = sea_query::Query::select()
54+
/// .column(Glyph::Id)
55+
/// .from(Glyph::Table)
56+
/// .cond_where(
57+
/// Cond::all()
58+
/// .add(Expr::col(Glyph::Aspect).eq(0).into_condition().not())
59+
/// .add(Expr::col(Glyph::Id).eq(0).into_condition().not()),
60+
/// )
61+
/// .to_string(PostgresQueryBuilder);
62+
/// assert_eq!(
63+
/// statement,
64+
/// r#"SELECT "id" FROM "glyph" WHERE (NOT ("aspect" = 0)) AND (NOT ("id" = 0))"#
65+
/// );
66+
/// ```
4967
#[allow(clippy::should_implement_trait)]
5068
pub fn add<C>(mut self, condition: C) -> Self
5169
where
@@ -58,7 +76,7 @@ impl Condition {
5876
return self;
5977
}
6078
// Skip the junction if there is only one.
61-
if c.conditions.len() == 1 {
79+
if c.conditions.len() == 1 && !c.negate {
6280
expr = c.conditions.pop().unwrap();
6381
}
6482
}

0 commit comments

Comments
 (0)