You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Support for foreign keys for nested structs would support SQL JOINs and more complicated/efficient querying. Foreign key support would be an alternative to dumping nested structs to a field as JSON.
Consider the following example:
use serde::{Deserialize,Serialize};use turbosql::Turbosql;#[derive(Turbosql,Default,Serialize,Deserialize)]pubstructDepartment{pubrowid:Option<i64>,pubemployees:Option<Vec<Employee>>,// nested structpubname:Option<String>,}#[derive(Turbosql,Default,Serialize,Deserialize)]pubstructEmployee{pubrowid:Option<i64>,pubdepartmentid:Option<i64>,// matches the rowid property from Department structpubfirst_name:Option<String>,publast_name:Option<String>,}pubfnmain() -> Result<i64, turbosql::Error>{let employee1 = Employee{departmentid:Some(0),first_name:Some("Bob".to_string()),last_name:Some("Johnson".to_string()),
..Default::default()};let employee2 = Employee{departmentid:Some(0),first_name:Some("Frank".to_string()),last_name:Some("Waltz".to_string()),
..Default::default()};let department = Department{employees:Some(vec![employee1, employee2]),name:Some("Sales".to_string()),
..Default::default()};
department.insert()}
Turbosql will generate the following SQL tables (with some extra stuff):
CREATETABLEemployee (
rowid INTEGERPRIMARY KEY,
departmentid INTEGER,
first_name TEXT,
last_name TEXT
);
CREATETABLEdepartment (
rowid INTEGERPRIMARY KEY,
name TEXT,
address TEXT,
employees TEXT-- contains a JSON dump of the vector of employees
);
But since we're only inserting the department object, the employee table is not populated. Instead, a JSON string is dumped into the department.employees field:
sqlite> SELECT * FROM DEPARTMENT;
1|Sales||[{"rowid":null,"departmentid":0,"first_name":"Bob","last_name":"Johnson"},{"rowid":null,"departmentid":0,"first_name":"Frank","last_name":"Waltz"}]
This JSON dump is not queryable with SQL. To support foreign keys, we currently have to manually insert each nested object:
// This sucks... :(fnput(department:&Department) -> Result<i64,crate::Error>{letmut dpt = department.clone();let dpt_employees = dpt.employees.clone();// Clear the employees so there's no JSON dump
dpt.employees = None;let rowid = dpt.insert()?;match dpt_employees {Some(employees) => {// Manually set the department id on each employee and insertfor emp in&employees{letmut e = emp.clone();
e.departmentid = Some(rowid);
e.insert()?;}},None => {},};Ok(rowid)}
Instead, there should be some technique for signaling table relationships to Turbosql. This could be an attribute macro that signals to Turbosql a field represents a foreign key from another table. If this macro is present, Turbosql should generate a foreign key field that references the parent table, and remove the respective field in the parent table. Ideally, Turbosql would also automatically insert these nested structs into the correct table when .insert() is called on the parent struct.
For example:
#[derive(Turbosql,Default,Serialize,Deserialize)]pubstructEmployee{pubrowid:Option<i64>,#[foreign_key(Department)]// new attribute macro signals to Turbosql this field represents a FOREIGN KEYpubdepartmentid:Option<i64>,pubfirst_name:Option<String>,publast_name:Option<String>,};
CREATETABLEemployee (
rowid INTEGERPRIMARY KEY,
FOREIGN KEY (departmentid) REFERENCES department(rowid), -- uses a FOREIGN KEY from the parent table "department"
first_name TEXT,
last_name TEXT
);
CREATETABLEdepartment ( -- no longer has "employees" field
rowid INTEGERPRIMARY KEY,
name TEXT,
address TEXT
);
This would enable JOINs in SQL for more advanced querying of related tables.
-- something like this...SELECT*FROM department
INNER JOIN employee
ONdepartment.rowid=employee.departmentid;
The text was updated successfully, but these errors were encountered:
Support for foreign keys for nested structs would support SQL JOINs and more complicated/efficient querying. Foreign key support would be an alternative to dumping nested structs to a field as JSON.
Consider the following example:
Turbosql will generate the following SQL tables (with some extra stuff):
But since we're only inserting the
department
object, the employee table is not populated. Instead, a JSON string is dumped into thedepartment.employees
field:This JSON dump is not queryable with SQL. To support foreign keys, we currently have to manually insert each nested object:
Instead, there should be some technique for signaling table relationships to Turbosql. This could be an attribute macro that signals to Turbosql a field represents a foreign key from another table. If this macro is present, Turbosql should generate a foreign key field that references the parent table, and remove the respective field in the parent table. Ideally, Turbosql would also automatically insert these nested structs into the correct table when
.insert()
is called on the parent struct.For example:
This would enable JOINs in SQL for more advanced querying of related tables.
The text was updated successfully, but these errors were encountered: