I have a model with two relations which i set them as lazy="joined". so i
was thinking while they are already joined, i can filter relation objects
without using join() in query. but i was wrong. this is what working for me:
query = session.query(MyModel) \
.join(BModel) \
.join(CModel) \
.options(
contains_eager(MyModel.b_relation),
contains_eager(MyModel.c_relation)
) \
.filter(...)
Thanks again.
On Tuesday, January 13, 2015 at 5:25:35 PM UTC+3:30, Simon King wrote:
>
> On Mon, Jan 12, 2015 at 10:45 AM, Mehdi <[email protected] <javascript:>>
> wrote:
> > Hi
> > I want to make a query with given filters received from client side.
> client
> > side sends a json array like this:
> > [
> > "0DA": {conditionType: null, column: "serialNumber", operator:
> > "GREATER_THAN", value: 50},
> > "1DG": {conditionType: "AND", column: "name", operator: "EQUAL",
> value:
> > "john" }
> > ]
> >
> > I've managed to convert the json array into string query with a template
> > like this:
> > " {condition_type} {column} {operator} {value} "
> >
> > But this is only the "WHERE" clause. for complete query i use
> > session.query(myModel) which myModel has two relationships with
> > lazy="joined" mix with text() for filters:
> > session.query(myModel).filter(text(sql_string))
> >
> > But the problem is the session.query() uses alias column names(with "AS"
> > keyword) which my filter part with exact column names couln'd find those
> > aliased columns.
> > So Is there a way to tell session.query() use the exact column names? or
> i
> > should change my way?
> > Thanks.
>
> Rather than using the column names, could you use attributes on
> myModel instead? For example:
>
> col = getattr(myModel, columnname)
> if operator = "GREATER_THAN":
> condition = col > value
> elif operator = "LESS_THAN":
> condition = col < value
> # etc.
>
> result = session.query(myModel).filter(condition)
>
> You may not even need the "if" statement:
>
> operators = {
> "GREATER_THAN": sqlalchemy.sql.operators.gt
> <http://www.google.com/url?q=http%3A%2F%2Fsqlalchemy.sql.operators.gt&sa=D&sntz=1&usg=AFQjCNHNXjtg_00KfVwfI-oe1gx9oWYFtA>,
>
>
> "LESS_THAN": sqlalchemy.sql.operators.lt,
> # etc.
> }
>
> col = getattr(myModel, columnname)
> condition = operators[operator](col, value)
>
> Hope that helps,
>
> Simon
>
--
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.