SurrealDB - extra fields are ignored in SCHEMAFULL
- We DEFINE a SCHEMAFULL table with several field.
- If we try to create an entry using an extra field that field will be silently ignored.
- see feature request
examples/surrealdb/schemafull-ignore-extra-fields/src/main.rs
use serde::{Deserialize, Serialize}; use surrealdb::engine::local::Mem; use surrealdb::sql::Thing; use surrealdb::Surreal; #[derive(Debug, Deserialize, Serialize)] struct Entry { id: Thing, number: u32, } #[derive(Debug, Deserialize, Serialize)] struct EntryWithName { id: Thing, number: u32, name: Option<String>, } #[tokio::main] async fn main() -> surrealdb::Result<()> { let dbh = Surreal::new::<Mem>(()).await?; dbh.use_ns("demo").use_db("demo").await?; dbh.query("DEFINE TABLE entry SCHEMAFULL").await?; dbh.query("DEFINE FIELD number ON TABLE entry TYPE int") .await?; let res = dbh.query("CREATE entry CONTENT { number: 42 };").await?; match res.check() { Ok(val) => println!("Success: {val:?}"), Err(err) => println!("Error: {err}"), } println!("---------"); let res = dbh .query("CREATE entry CONTENT { number: 19, name: 'NineTeen' };") .await?; match res.check() { Ok(val) => println!("Success: {val:?}"), Err(err) => println!("Error: {err}"), } println!("---------"); let mut entries = dbh.query("SELECT * FROM entry").await?; let entries: Vec<Entry> = entries.take(0)?; for entry in entries { println!("{} {}", entry.id, entry.number); } println!("---------"); let mut entries = dbh.query("SELECT * FROM entry").await?; let entries: Vec<EntryWithName> = entries.take(0)?; for entry in entries { println!( "{} {} {}", entry.id, entry.number, entry.name.unwrap_or(String::from("NO NAME")) ); } println!("---------"); Ok(()) }
Success: Response { client: Surreal { router: OnceLock(Router { sender: Sender { .. }, last_id: 4, features: {LiveQueries, Backup} }), engine: PhantomData<surrealdb::api::engine::any::Any> }, results: {0: (Stats { execution_time: Some(1.279388ms) }, Ok(Array(Array([Object(Object({"id": Thing(Thing { tb: "entry", id: String("5rw5ezjzvm6rf5nrc9xw") }), "number": Number(Int(42))}))]))))}, live_queries: {} } --------- Success: Response { client: Surreal { router: OnceLock(Router { sender: Sender { .. }, last_id: 5, features: {LiveQueries, Backup} }), engine: PhantomData<surrealdb::api::engine::any::Any> }, results: {0: (Stats { execution_time: Some(1.046842ms) }, Ok(Array(Array([Object(Object({"id": Thing(Thing { tb: "entry", id: String("wz9sxmpax344kr8u3c8s") }), "number": Number(Int(19))}))]))))}, live_queries: {} } --------- entry:5rw5ezjzvm6rf5nrc9xw 42 entry:wz9sxmpax344kr8u3c8s 19 --------- entry:5rw5ezjzvm6rf5nrc9xw 42 NO NAME entry:wz9sxmpax344kr8u3c8s 19 NO NAME ---------