Prevent accidental insertion of full value in place of references.
When creating and using a reference in a field, the id of the Node record should be used rather than the full value itself.
We want to be able to insert only ids but potentially get Id or FetchedValue or Null when we select or fetch from the database but not when we create.
e.g: LinkOne::from(course) or course.into() makes sure course id is returned rather than the entire course struct
#[derive(SurrealdbModel, TypedBuilder, Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Student {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
id: Option<String>,
first_name: String,
#[surrealdb(reference_one = "Course", skip_serializing)]
course: LinkOne<Course>,
#[surrealdb(reference_one = "Course", skip_serializing)]
#[serde(rename = "lowo")]
all_semester_courses: LinkMany<Course>,
}
#[derive(SurrealdbModel, TypedBuilder, Default, Serialize, Deserialize, Debug, Clone)]
#[surrealdb(rename_all = "camelCase")]
pub struct Course {
#[serde(skip_serializing_if = "Option::is_none")]
#[builder(default, setter(strip_option))]
id: Option<String>,
title: String,
}
fn main {
let course: Option<Course> = DB
.select(SurIdComplex::from_string(cx.clone().id.unwrap()))
// .select(SurIdComplex(("Course".to_string(), cx.id.unwrap()).0))
.await
.unwrap();
let course = course.as_ref().unwrap();
// let course = &course.unwrap();
let student = query
.first_name("dayo".into())
.course(course.into()) // Same as LinkOne::from(course). This makes sure courseid is returned rather than the entire course struct
.all_semester_courses(vec![course.into(), course.into()])
.build();
}
Purpose
LinkOne
andLinkMany
for foreign fieldsId
orFetchedValue
orNull
when weselect
or fetch from the database but not when we create.e.g:
LinkOne::from(course)
orcourse.into()
makes surecourse id
is returned rather than the entire course struct