diff --git a/lib/src/doc/insert.rs b/lib/src/doc/insert.rs index 65f02323..75ed8fd0 100644 --- a/lib/src/doc/insert.rs +++ b/lib/src/doc/insert.rs @@ -60,10 +60,10 @@ impl<'a> Document<'a> { self.clean(ctx, opt, txn, stm).await?; // Check if allowed self.allow(ctx, opt, txn, stm).await?; - // Store record data - self.store(ctx, opt, txn, stm).await?; // Store index data self.index(ctx, opt, txn, stm).await?; + // Store record data + self.store(ctx, opt, txn, stm).await?; // Run table queries self.table(ctx, opt, txn, stm).await?; // Run lives queries @@ -95,10 +95,10 @@ impl<'a> Document<'a> { self.clean(ctx, opt, txn, stm).await?; // Check if allowed self.allow(ctx, opt, txn, stm).await?; - // Store record data - self.store(ctx, opt, txn, stm).await?; // Store index data self.index(ctx, opt, txn, stm).await?; + // Store record data + self.store(ctx, opt, txn, stm).await?; // Run table queries self.table(ctx, opt, txn, stm).await?; // Run lives queries diff --git a/lib/tests/insert.rs b/lib/tests/insert.rs index 9fb6b60a..4025d7c1 100644 --- a/lib/tests/insert.rs +++ b/lib/tests/insert.rs @@ -488,3 +488,32 @@ async fn check_permissions_auth_disabled() { assert!(res.unwrap() != Value::parse("[]"), "{}", "anonymous user should be able to insert a new record if the table exists and grants full permissions"); } } + +#[tokio::test] +async fn insert_statement_unique_index() -> Result<(), Error> { + let sql = " + DEFINE INDEX name ON TABLE company COLUMNS name UNIQUE; + INSERT INTO company { name: 'SurrealDB' }; + INSERT INTO company { name: 'SurrealDB' }; + SELECT count() FROM company GROUP ALL; + "; + let dbs = new_ds().await?; + let ses = Session::owner().with_ns("test").with_db("test"); + let res = &mut dbs.execute(sql, &ses, None).await?; + assert_eq!(res.len(), 4); + // + let tmp = res.remove(0).result; + assert!(tmp.is_ok()); + // + let tmp = res.remove(0).result; + assert!(tmp.is_ok()); + // + let tmp = res.remove(0).result; + assert!(tmp.is_ok()); + // + let tmp = res.remove(0).result?; + let val = Value::parse("[ { count: 1 } ]"); + assert_eq!(tmp, val); + // + Ok(()) +}