mod employee { #[derive(Debug, elephantry::Entity)] #[elephantry(model = "Model", structure = "Structure", relation = "employee")] pub struct Entity { #[elephantry(pk)] pub employee_id: Option, pub first_name: String, pub last_name: String, pub birth_date: chrono::NaiveDate, pub is_manager: bool, pub day_salary: bigdecimal::BigDecimal, pub department_id: i32, } impl Model { pub fn new_employee( &self, employee: &Entity, department: &str, ) -> elephantry::Result { let transaction = self.connection.transaction(); transaction.start()?; transaction.set_deferrable( Some(vec!["employee_department_id_fkey"]), elephantry::transaction::Constraints::Deferred, )?; let mut employee = self.connection.insert_one::(employee)?; let department = self .connection .find_where::("name = $*", &[&department], None)? .nth(0) .unwrap(); employee.department_id = department.department_id; let employee = self .connection .update_one::( &elephantry::pk! { employee_id => employee.employee_id }, &employee, )? .unwrap(); transaction.commit()?; Ok(employee) } } } mod department { #[derive(Debug, elephantry::Entity)] #[elephantry(model = "Model", structure = "Structure", relation = "department")] pub struct Entity { #[elephantry(pk)] pub department_id: i32, pub name: String, pub parent_id: Option, } } fn main() -> elephantry::Result { env_logger::init(); let database_url = std::env::var("DATABASE_URL").unwrap_or_else(|_| "postgres://localhost".to_string()); let elephantry = elephantry::Pool::new(&database_url)?; elephantry.execute(include_str!("structure.sql"))?; let employee = elephantry.model::().new_employee( &employee::Entity { employee_id: None, first_name: "First name".to_string(), last_name: "Last name".to_string(), birth_date: chrono::NaiveDate::from_ymd_opt(1990, 1, 1).unwrap(), is_manager: true, day_salary: 1_000.into(), department_id: -1, }, "Direction", )?; dbg!(employee); Ok(()) }