Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed incorrect relation alias namespacing issue #12

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions model/src/crud/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -555,8 +555,10 @@ fn generate_join_clause<T: Model>(vars: &Vec<Var>) -> Result<String, Error> {
let mut seen = HashSet::new();
let mut join_clauses = vec![];

let table_name = T::table_name();

for var in vars.iter() {
let joins = joins_from_var(&T::table_name(), var, &T::definition())?;
let joins = joins_from_var(&table_name, &table_name, var, &T::definition())?;

for (relation, join_clause) in joins.iter() {
if seen.insert(relation.clone()) {
Expand All @@ -569,6 +571,7 @@ fn generate_join_clause<T: Model>(vars: &Vec<Var>) -> Result<String, Error> {
}

fn joins_from_var(
root: &str,
parent: &str,
var: &Var,
model_def: &ModelDef,
Expand All @@ -583,13 +586,14 @@ fn joins_from_var(
.ok_or_else(|| Error::bad_request("undefined field"))?;

let id_field_name = (model_def.id_field_name)();
let join_clause = relation_def.to_join_clause(&parent, &id_field_name);
let join_clause = relation_def.to_join_clause(&parent, &id_field_name, root == parent);

let next_parent = format!("{}_{}", parent, name);

let mut res = vec![(next_parent.clone(), join_clause)];

res.extend(joins_from_var(
&root,
&next_parent,
var.as_ref(),
&relation_def.model_definition,
Expand Down
33 changes: 13 additions & 20 deletions model/src/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,33 +73,27 @@ impl RelationDef {
}
}

pub fn to_join_clause(&self, parent: &str, parent_id_column: &str) -> String {
pub fn to_join_clause(&self, parent: &str, parent_id_column: &str, is_root: bool) -> String {
let related_table = (self.model_definition.table_name)();
let related_id_column = (self.model_definition.id_field_name)();

let alias = if is_root {
self.name.clone()
} else {
format!("{}_{}", parent, self.name)
};

match &self.reference {
Reference::From(column) => {
format!(
"LEFT JOIN {} AS {}_{} ON {}.{} = {}.{}",
related_table,
parent,
related_table,
parent,
column,
related_table,
related_id_column
"LEFT JOIN {} AS {} ON {}.{} = {}.{}",
related_table, alias, parent, column, related_table, related_id_column
)
}
Reference::To(column) => {
format!(
"LEFT JOIN {} AS {}_{} ON {}.{} = {}.{}",
related_table,
parent,
related_table,
parent,
parent_id_column,
related_table,
column,
"LEFT JOIN {} AS {} ON {}.{} = {}.{}",
related_table, alias, parent, parent_id_column, related_table, column,
)
}
Reference::Via((junction_table, from_reference, to_reference)) => {
Expand All @@ -115,10 +109,9 @@ impl RelationDef {
);

let join_relation = format!(
"INNER JOIN {} AS {}_{} ON {}_{}.{} = {}.{}",
related_table,
parent,
"INNER JOIN {} AS {} ON {}_{}.{} = {}.{}",
related_table,
alias,
parent,
junction_table,
to_reference,
Expand Down