How i can use column_as
for select alias table
#2208
Unanswered
BlackSoulHub
asked this question in
Q&A
Replies: 1 comment
-
I found decision, but it's looks like bad. I use idea from this comment: #802 (comment) I create next struct: struct Unquoted(String); And implement impl Iden for Unquoted {
fn unquoted(&self, s: &mut dyn Write) {
// just write inner field to writer...
write!(s, "{}", self.0).expect("Unquoted error")
}
} And create convenient API for this: impl Unquoted {
fn from_str(str: &str) -> DynIden {
DynIden::new(Self(str.to_string()))
}
fn table_column(table: &str, column: &str) -> ColumnRef {
ColumnRef::TableColumn(Unquoted::from_str(table), Unquoted::from_str(column))
}
} Usage: email::Entity::find()
.column_as(
SimpleExpr::Column(Unquoted::table_column("t_receiver", "id")),
"receiver_id",
)
.column_as(
SimpleExpr::Column(Unquoted::table_column("t_receiver", "address")),
"receiver_addr",
)
.column_as(
SimpleExpr::Column(Unquoted::table_column("t_receiver", "role")),
"receiver_role",
)
.column_as(
SimpleExpr::Column(Unquoted::table_column("t_sender", "id")),
"sender_id",
)
.column_as(
SimpleExpr::Column(Unquoted::table_column("t_sender", "address")),
"sender_addr",
)
.column_as(
SimpleExpr::Column(Unquoted::table_column("t_sender", "role")),
"sender_role",
)
.join_as(
JoinType::LeftJoin,
email::Entity::belongs_to(mailbox::Entity)
.from(email::Column::Receiver)
.to(mailbox::Column::Id)
.into(),
Alias::new("t_receiver"),
)
.join_as(
JoinType::LeftJoin,
email::Entity::belongs_to(mailbox::Entity)
.from(email::Column::Sender)
.to(mailbox::Column::Id)
.into(),
Alias::new("t_sender"),
)
.filter(email::Column::OwnerMailbox.eq(owner_mbox_id))
.offset(offset)
.limit(limit); It works for me, but maybe there is a better way to do it? |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello all!
I need to get next model from query:
How i can do it?
I try this, but it isn't work:
In docs i find
But i didn't find how i can create
Expr
with correct table prefix.Maybe i can create create Column enum on fly?
I don't find decision in docs or examples.
Beta Was this translation helpful? Give feedback.
All reactions