Python SQLAlchemy joinedload filter parameters - python

I'm trying to make a left outer join relation between two tables using joinedload but it doesn't seems to have the same behavior as using outerjoin method.
Here is an example using outerjoin:
stmt = select(ServerFarm)
stmt = stmt.outerjoin(
Server,
(ServerFarm.name == Server.server_farm_id) & (Server.deleted == False)
)
stmt = stmt.filter(ServerFarm.name == name)
print(stmt)
# SELECT ... FROM server_farm
# LEFT JOIN server ON server_farm.name = server.server_farm_id and server.deleted = False
# WHERE server_farm.name = :name
So when I'm using joinedload this is what happens:
stmt = select(ServerFarm)
stmt = stmt.options(
joinedload(
ServerFarm.servers,
(ServerFarm.name == Server.server_farm_id) & (Server.deleted == False)
)
)
stmt = stmt.filter(ServerFarm.name == name)
# SELECT ... FROM server_farm
# LEFT JOIN server ON server_farm.name = server.server_farm_id
# WHERE server_farm.name = :name
On this case, it just ignores the (Server.deleted == False) option. What am I doing wrong? Is there a way to add a "custom" filter on a left outer join using joinedload? How?

Related

Translane sql to sqlalchemy core

I have this sql
SELECT publishers.id, publishers.created_at, publishers.updated_at, publishers.name,
count(case books.is_active when true then 1 else null end) AS is_active,
count(books.id) as book_count
FROM publishers
LEFT OUTER JOIN books ON books.publisher_id = publishers.id
GROUP BY publishers.id
How i can use this on my python-project with sqlalchemy core?
I have this code now
book_count_alias = models.Book.__table__.alias('book_alias')
join = cls._model.__table__.outerjoin(
models.Book.__table__,
sa.and_(models.Book.publisher_id == models.Publisher.id, models.Book.is_active == True)
).outerjoin(
book_count_alias, book_count_alias.c.publisher_id == models.Publisher.id
)
query = sa.select([cls._model, sa.func.count(models.Book.id).label('is_active'),
sa.func.count(book_count_alias.c.id).label('book_count')])
if q:
query = query.where(cls._model.name.ilike(f'%{q}%'))
query = query.select_from(join).group_by(cls._model.id)
results = await database.fetch_all(query)
return [cls._get_parsed_object(x, schema=schemas.PublisherWithBookCountAndIsActive) for x in results
]

SQLAlchemy appends aliased Table in FROM clause

I'm creating an application with a flask backend and I'm using SQLAlchemy Core to create the SQL-Queries. Now I'm creating a base select like this:
SYSTEM = self.__TABLES.SYSTEM
RESPONSIBLE = self.__TABLES.responsible
INFRA_RESPONSIBLE = RESPONSIBLE.alias('INFRA_RES')
SYSTEM_RESPONSIBLE = RESPONSIBLE.alias('SYS_RES')
STMT = SYSTEM \
.outerjoin(INFRA_RESPONSIBLE, INFRA_RESPONSIBLE.c.id == SYSTEM.c.infrares) \
.outerjoin(SYSTEM_RESPONSIBLE, SYSTEM_RESPONSIBLE.c.id == SYSTEM.c.sysres)
Now, if I'm creating a SQL-Query like this:
STMT = select([SYSTEM]) \
.select_from(STMT) \
.where(INFRA_RESPONSIBLE.c.id == 1)
This is how the generated looks like:
SELECT "it".systems.*
FROM "it".responsible AS "INFRA_RES", "it".systems
LEFT OUTER JOIN "it".responsible as "INFRA_RES" ON "INFRA_RES".id = "it".systems.infrares
LEFT OUTER JOIN "it".responsible as "SYS_RES" ON "SYS_RES".id = "it".systems.sysres
WHERE "INFRA_RES".id = 1
But i want to look it like this:
SELECT "it".systems.*
"it".systems
LEFT OUTER JOIN "it".responsible as "INFRA_RES" ON "INFRA_RES".id = "it".systems.infrares
LEFT OUTER JOIN "it".responsible as "SYS_RES" ON "SYS_RES".id = "it".systems.sysres
WHERE "INFRA_RES".id = 1
The table INFRA_RES is already joined with the base statement, how can I achieve to build a proper SQL-Statement?

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)
)
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()

Query multiple joins sqlalchemy

I am generating a query with sql alchemy part by part. I have this object that is working very well for a query with only one join:
**I have an ORM model, but I cannot use primary keys setted because are not real.
q = select( self.selectObj._select
).select_from(
self.joinObj._join
).where(
and_(*self.whereObj._where)
).group_by(
*self.selectObj._groupby
).order_by(
self.selectObj._orderby
).limit(
self.selectObj._limit
).having(
self.selectObj._having
)
I have this method for generate the joins:
def get_joins(self, first, leftTable, rightTable, leftTableColumn, rightTableColumn, outer):
if first:
self._join = join(leftTable, rightTable, leftTableColumn == rightTableColumn, full=outer)
first = False
else:
self._join = self._join + join(leftTable, rightTable, leftTableColumn == rightTableColumn, full=outer)
I donĀ“t know, how can I generate, concatenate, get, etc two or more joins for use it in the select_from clause. Any idea?
Thanks a lot in advance :)
The final result should be like this in the from:
SELECT a.dev, b.asha, c.unk
FROM a
FULL OUTER JOIN b ON a.dev = b.devicb
FULL OUTER JOIN c ON a.dev = c.devicc
WHERE
a.cust = 'SNTC' AND
b.cust = 'SNTC' AND
c.cust = 'SNTC' AND
a.invent = '10' AND
b.invent = '10' AND
c.invent = '10'
I solved in this way, just invoquing .join in the previous join but only using right table. self._join.join(rightTable,...)
Complete solution for the method:
def get_joins(self, first, leftTable, rightTable, leftTableColumn, rightTableColumn, outer):
if first:
self._join = join(leftTable, rightTable, leftTableColumn == rightTableColumn, full=outer)
first = False
else:
self._join = self._join.join(rightTable, leftTableColumn == rightTableColumn, full=outer)

Sqlalchemy: How to perform outer join with itself?

I want to perform this SQL query using Sqlalchemy (with model Evaluation):
select e1.user, sum(e1.points) as s from
(select e1.*
from evaluations e1 left outer join evaluations e2
on (e1.user = e2.user and e1.module = e2.module and e1.time < e2.time)
where e2.user is null and e1.module in (__another subquery__))
group by e1.user order by s limit 5
I don't know how to perform left outer join (especialy the renaming and referencing of renamed comlumns). Could you help me?
# sample sub-query for testing
_another_query = session.query(Evaluation.module).filter(Evaluation.module > 3)
# define aliases
E1 = aliased(Evaluation, name="e1")
E2 = aliased(Evaluation, name="e2")
# inner query
sq = (
session
# .query(E1)
# select columns explicitely to control labels
.query(E1.user.label("user"), E1.points.label("points"))
.outerjoin(E2, and_(
E1.user == E2.user,
E1.module == E2.module,
E1.time < E2.time,
))
.filter(E2.user == None)
.filter(E1.module.in_(_another_query))
)
sq = sq.subquery(name="sq")
# now lets group by
q = (
session
.query(sq.c.user, func.sum(sq.c.points))
.group_by(sq.c.user)
.order_by(func.sum(sq.c.points))
.limit(5)
)

Categories

Resources