-
Let's have a "simple" query like SELECT * FROM table1 INNER JOIN table1 AS table2 ON table1.field = table2.id; How should I represent it using crate's types? Query::select()
.from(Entity)
.join_as(
JoinType::LeftJoin,
Entity,
Alias::new("table2"),
SimpleExpr::Column({
let (a, b) = Column::Field.as_column_ref();
ColumnRef::TableColumn(a, b)
}).equals(SimpleExpr::Custom("`table2`.`id`".to_owned())
)) Am I missing some simplier way? |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments
-
Probably. How about these? https://www.sea-ql.org/SeaORM/docs/advanced-query/more-join Depending whether you want to use |
Beta Was this translation helpful? Give feedback.
-
Wait, the core of my issue is about joining aliased tables, while joining standard tables everything works smooth, but adding an alias requires adding a lot of code and rely con custom expression, that on my way of view decreases the security given by an ORM system. Column::Field.equals(Expr::tbl(Alias::new("table2"), Column::Id)) The problem here is that |
Beta Was this translation helpful? Give feedback.
-
Okay sorry but I am confused. So you want to use an alias as the table name? Can you use a unit struct instead? #[derive(Iden)]
pub struct Table2; |
Beta Was this translation helpful? Give feedback.
-
Maybe I'm not getting it, but this doesn't seems to solve the problem. |
Beta Was this translation helpful? Give feedback.
-
I might not exactly get your scenario, but here is an example from SeaQuery #[derive(Iden)]
pub struct MyAlias;
let query = Query::select()
.from(Char::Table)
.join_as(
JoinType::RightJoin,
Font::Table,
MyAlias,
Expr::tbl(Font::Table, Font::Id).equals(MyAlias, Char::FontId)
)
.to_owned(); |
Beta Was this translation helpful? Give feedback.
-
Yeah, you're right, I'm making confusion between eq and equals... |
Beta Was this translation helpful? Give feedback.
-
Yes, to clarify, eq is for comparing values while equals is with another expression.
|
Beta Was this translation helpful? Give feedback.
I might not exactly get your scenario, but here is an example from SeaQuery