Unique index should ignore none
or null
values (#2313)
This commit is contained in:
parent
a2f4d6776d
commit
b66e537f98
3 changed files with 28 additions and 3 deletions
|
@ -164,9 +164,11 @@ impl<'a> IndexOperation<'a> {
|
|||
}
|
||||
// Create the new index data
|
||||
if let Some(n) = &self.n {
|
||||
let key = self.get_unique_index_key(n);
|
||||
if run.putc(key, self.rid, None).await.is_err() {
|
||||
return self.err_index_exists(n);
|
||||
if !n.is_all_none_or_null() {
|
||||
let key = self.get_unique_index_key(n);
|
||||
if run.putc(key, self.rid, None).await.is_err() {
|
||||
return self.err_index_exists(n);
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
|
|
|
@ -134,6 +134,10 @@ impl Array {
|
|||
}
|
||||
Ok(Value::Array(x))
|
||||
}
|
||||
|
||||
pub(crate) fn is_all_none_or_null(&self) -> bool {
|
||||
self.0.iter().all(|v| v.is_none_or_null())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Array {
|
||||
|
|
|
@ -100,3 +100,22 @@ async fn create_with_id() -> Result<(), Error> {
|
|||
//
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn create_on_non_values_with_unique_index() -> Result<(), Error> {
|
||||
let sql = "
|
||||
DEFINE INDEX national_id_idx ON foo FIELDS national_id UNIQUE;
|
||||
CREATE foo SET name = 'John Doe';
|
||||
CREATE foo SET name = 'Jane Doe';
|
||||
";
|
||||
|
||||
let dbs = Datastore::new("memory").await?;
|
||||
let ses = Session::for_kv().with_ns("test").with_db("test");
|
||||
let res = &mut dbs.execute(sql, &ses, None).await?;
|
||||
assert_eq!(res.len(), 3);
|
||||
//
|
||||
for _ in 0..3 {
|
||||
let _ = res.remove(0).result?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue