How to construct a complex LEFT JOIN with aliasing using sea-query in Rust?

449 views Asked by At

I'm working on a Rust project where I use sea-query to build SQL queries. I have the following SQL query:

SELECT * FROM tblcomponent AS com1 
LEFT JOIN tblcomponent AS com2 ON com1.intParentComponentId_fk=com2.intId_pk 
LEFT JOIN tblcomponent AS com3 ON com2.intParentComponentId_fk=com3.intId_pk 
LEFT JOIN tblcomponent AS com4 ON com3.intParentComponentId_fk=com4.intId_pk 
LEFT JOIN tblcomponentdef AS comdef2 ON com2.intComponentDefId_fk=comdef2.intId_pk
LEFT JOIN tblcomponentdef AS comdef3 ON com3.intComponentDefId_fk=comdef3.intId_pk
LEFT JOIN tblcomponentdef AS comdef4 ON com4.intComponentDefId_fk=comdef4.intId_pk

The main challenge I'm facing is to set up multiple LEFT JOIN clauses using the same table (tblcomponent) with different aliases (com1, com2, com3, com4).

Does anyone know how to replicate the above SQL using sea-query in Rust?

As you can see it is a nested self-join. That is why we need multiple aliases. Any help would be greatly appreciated!

This is kinda how it looks like

use entities::{tblcomponent,tblcomponentdef}
tblcomponent::Entity::find()
        .join(
            JoinType::LeftJoin,
            //???
todo!()
        )
0

There are 0 answers