g.breeze
g.breeze

Reputation: 1967

python peewee dynamically or + and clauses

I'd like to do a and clause with two lists of multiple or clauses from the same table.

The problem with the following code is, that the query result is empty. If I just select 'indices' or 'brokers', the result is fine.

        ...
        query = query.join(StockGroupTicker, on=(Ticker.id == StockGroupTicker.ticker))
        # indices
        if "indices" in filter:
            where_indices = []
            for f in filter["indices"]:
                where_indices.append(StockGroupTicker.stock_index == int(f))
            if len(where_indices):
                query = query.where(peewee.reduce(peewee.operator.or_, where_indices))

        # broker
        if "brokers" in filter:
            where_broker = []
            for f in filter["brokers"]:
                where_broker.append(StockGroupTicker.stock_index == int(f))
            if len(where_broker):
                query = query.where(peewee.reduce(peewee.operator.or_, where_broker))

    return query.distinct()

SQL Querie (update)


# index and brocker

SELECT
    DISTINCT `t1`.`id`,
    `t1`.`symbol`,
    `t1`.`type`,
    `t1`.`name`,
    `t1`.`sector`,
    `t1`.`region`,
    `t1`.`primary_exchange`,
    `t1`.`currency`,
    `t1`.`score`,
    `t1`.`last_price`,
    `t1`.`last_price_date`,
    `t1`.`last_price_check`,
    `t1`.`last_stock_split`,
    `t1`.`next_earning`,
    `t1`.`last_earnings_update`,
    `t1`.`disused`,
    `t1`.`source`,
    `t1`.`source_intraday`,
    `t1`.`created`,
    `t1`.`modified`,
    `t2`.`invest_score` AS `invest_score`
FROM
    `ticker` AS `t1`
INNER JOIN `tickerstats` AS `t2` ON
    (`t1`.`id` = `t2`.`ticker_id`)
INNER JOIN `stockgroupticker` AS `t3` ON
    (`t1`.`id` = `t3`.`ticker_id`)
WHERE
    (((((`t1`.`disused` IS NULL)
        OR (`t1`.`disused` = 0))
        AND (`t2`.`volume_mean_5` > 10000.0))
        AND (`t3`.`stock_index_id` = 1))
        AND (`t3`.`stock_index_id` = 10)
        )
        

enter image description here

Upvotes: 1

Views: 389

Answers (1)

g.breeze
g.breeze

Reputation: 1967

Thanks to @coleifer, the peewee solution is quite simple. I had to use an alias.

if "indices" in filter and filter["indices"]:
    query = query.join(
        StockGroupTicker, peewee.JOIN.INNER, on=(Ticker.id == StockGroupTicker.ticker)
    )
    where_indices = []
    for f in filter["indices"]:
        where_indices.append(StockGroupTicker.stock_index == int(f))
    if len(where_indices):
        query = query.where(peewee.reduce(peewee.operator.or_, where_indices))

if "brokers" in filter and filter["brokers"]:
    BrokerGroupTicker = StockGroupTicker.alias()
    query = query.join(
        BrokerGroupTicker, peewee.JOIN.INNER, on=(Ticker.id == BrokerGroupTicker.ticker)
    )
    where_broker = []
    for f in filter["brokers"]:
        where_broker.append(BrokerGroupTicker.stock_index == int(f))
    if len(where_broker):
        query = query.where(peewee.reduce(peewee.operator.or_, where_broker))

return query.distinct()

Upvotes: 1

Related Questions