-
-
Notifications
You must be signed in to change notification settings - Fork 520
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
find_linked()
generates invalid SQL for self referencing associations
#163
Comments
Thanks. Can you give a more concrete example on your entity and query? |
Example join table entity:
Example queries:
|
find_linked()
generates invalid SQL for self referencing associations
Sorry for not able to make it into |
Would adding |
If you are talking |
If that helps, I use this trait to workaround the issue (also has some unrelated filtering logic, but you get the idea): pub trait FindLinkedFrom<E: EntityTrait> {
fn find_linked_from_id<L: Linked<ToEntity=E>>(self, id: Key<L::FromEntity>) -> Self;
}
impl<E: EntityTrait> FindLinkedFrom<E> for Select<E> {
fn find_linked_from_id<L: Linked<ToEntity=E>>(self, id: Key<L::FromEntity>) -> Self
{
let mut res = self;
let link = L::default().link();
let count = link.len();
for (i, rel) in link.into_iter().rev().enumerate() {
let from_tbl = rel.from_tbl.clone();
let from_tbl_alias = Alias::new(&format!("r{}", i));
let from_tbl_iden = from_tbl_alias.clone().into_iden();
let to_tbl_iden = if i == 0 {
unpack_table_ref(&rel.to_tbl)
} else {
Alias::new(&format!("r{}", i - 1)).into_iden()
};
let mut condition = Condition::all()
.add(join_condition(rel, from_tbl_iden.clone(), to_tbl_iden));
if i >= count - 1 {
condition = condition.add(condition_filter_by_id::<L::FromEntity, _>(from_tbl_iden.clone(), &id));
}
QuerySelect::query(&mut res)
.join_as(JoinType::InnerJoin, from_tbl, from_tbl_alias, condition);
}
res
}
} |
The implementation of
find_linked()
does not use aliases on join statements, so for self referencing many to many associations such asTABLE -> RELATES_TO -> TABLE
the generated query is invalid. Other ORMs typically generate autoincremented aliases for each relation in this case.The text was updated successfully, but these errors were encountered: