<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Paul on Medium]]></title>
        <description><![CDATA[Stories by Paul on Medium]]></description>
        <link>https://medium.com/@kagiri?source=rss-1d38b9d262a4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*ctshDdQGmMfOevFmq8kNUg.png</url>
            <title>Stories by Paul on Medium</title>
            <link>https://medium.com/@kagiri?source=rss-1d38b9d262a4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sun, 06 Dec 2026 08:50:30 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@kagiri/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[Flask-SqlAlchemy nested queries]]></title>
            <link>https://medium.com/@kagiri/flask-sqlalchemy-nested-queries-64278f1ef39c?source=rss-1d38b9d262a4------2</link>
            <guid isPermaLink="false">https://medium.com/p/64278f1ef39c</guid>
            <category><![CDATA[sqlalchemy]]></category>
            <category><![CDATA[python-programming]]></category>
            <category><![CDATA[flask-sqlalchemy]]></category>
            <category><![CDATA[flask]]></category>
            <dc:creator><![CDATA[Paul]]></dc:creator>
            <pubDate>Mon, 03 Jul 2017 15:11:59 GMT</pubDate>
            <atom:updated>2017-07-03T15:11:59.661Z</atom:updated>
            <content:encoded><![CDATA[<p>Today I was researching how to go about writing queries such as this in Flask-SqlAlchemy:</p><pre>SELECT<br>  u.id, u.first_name, u.last_name, u.phone_number, u.email, ac.*<br>FROM users u<br>LEFT OUTER JOIN (<br>                  SELECT<br>                    a.user_id,<br>                    COALESCE(sum(a.points), 0)  AS all_points,<br>                    COALESCE(sum(a.cost), 0) AS all_costs<br>                    FROM accounts a<br>                    GROUP BY a.user_id<br>                  )<br>                  ac ON u.id = ac.user_id<br>WHERE u.role = 1<br>ORDER BY u.created_at DESC;</pre><p>…it gave me a hell of a time so I decided I would post it here for everyone.</p><pre>accounts_query = db.session.query(<br>           Account.user_id,<br>           func.coalesce(func.sum(Account.points), 0).label(&#39;all_points&#39;),<br>           func.coalesce(func.sum(Account.costs), 0).label(&#39;all_costs&#39;)<br>       ).group_by(Account.user_id).subquery()</pre><pre>users = db.session.query(<br>   User.id, User.first_name, User.last_name, User.phone_number, User.email<br>   accounts_query.c.all_points, accounts_query.c.all_costs<br>) \<br>   .outerjoin(accounts_query, User.id == accounts_query.c.user_id) \<br>   .filter(User.role == 1) \<br>   .order_by(User.created_at.desc()) \<br>   .all()</pre><p>The hardest part to figure was this bit <em>accounts_query.c.user_id</em>, I mean seriously, how is one supposed to figure that out SqlAlchemy?</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=64278f1ef39c" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>