SQLAlchemy 按 Flask 框架和 postgresQL 查询分组

SQLAlchemy Group by query with flask framework and postgresSQL

我在确定正确的查询以在我的 Flask 应用程序上使用 sqlalchemy 获得我想要的结果时遇到问题。我需要查询 return 结果作为字典列表,如下代码

[{
    "city": "San Francisco",
    "state": "CA",
    "venues": [{
        "id": 1,
        "name": "The Musical Hop",
        "num_upcoming_shows": 0,
    }, {
        "id": 3,
        "name": "Park Square Live Music & Coffee",
        "num_upcoming_shows": 1,
    }]
}, {
    "city": "New York",
    "state": "NY",
    "venues": [{
        "id": 2,
        "name": "The Dueling Pianos Bar",
        "num_upcoming_shows": 0,
    }]
}]

我正在尝试 return 数据库中的场馆列表,但现在按他们的州和城市分组并加入演出 table 以获得即将举行的演出的数量具体地点如上表所示。

为了解释上面的字典列表,查询是 returning 场地 table 中的 2 列,它们是州和城市,然后是别名 call venues 也 return 是一个字典列表,其中包含该州和城市的场馆名称,以及每个场馆根据与演出的关系即将举行的演出数量 table。

我有 3 个模型 tables 场馆、艺术家和表演,它们之间存在多对多关系

class Show(db.Model):
 __tablename__ = "shows"
 id = db.Column(db.Integer, primary_key=True)
 artist_id = db.Column(db.Integer, db.ForeignKey('artists.id'))
 venue_id = db.Column(db.Integer, db.ForeignKey('venues.id'))
 start_time = db.Column(db.DateTime(timezone=True))
 artist = db.relationship("Artist", backref=db.backref("shows", cascade="all, delete-orphan"))
 venue = db.relationship("Venue", backref=db.backref("shows", cascade="all, delete-orphan"))



class Artist(db.Model):
  __tablename__ = 'artists'

id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(), nullable=False)
description = db.Column(db.String(), nullable=False)
city = db.Column(db.String(120), nullable=False)
state = db.Column(db.String(120), nullable=False)
phone = db.Column(db.String(120), nullable=False)
genres = db.Column(db.String(), nullable=False)
image_link = db.Column(db.String(500), nullable=False)
facebook_link = db.Column(db.String(120), nullable=False)
website_link = db.Column(db.String(300), nullable=False)
looking_for_venue = db.Column(db.Boolean, nullable=False, default=False)

class Venue(db.Model):
  __tablename__ = 'venues'

 id = db.Column(db.Integer, primary_key=True)
 name = db.Column(db.String(), nullable=False)
 description = db.Column(db.String(), nullable=True)
 genres = db.Column(db.String(), nullable=False)
 city = db.Column(db.String(120), nullable=False)
 state = db.Column(db.String(120), nullable=False)
 address = db.Column(db.String(300), nullable=False)
 phone = db.Column(db.String(120), nullable=True, unique=True)
 image_link = db.Column(db.String(500), nullable=True)
 facebook_link = db.Column(db.String(300), nullable=False)
 website_link = db.Column(db.String(300), nullable=True)
 looking_for_talent = db.Column(db.Boolean, nullable=False, default=False) 

如果有人能帮助我解决 SQL 查询以及 SQLAlchemy 查询

,我将不胜感激

SQL查询:

select v.city, v.state, v.name, v.id, count(v.id)
from show s
join venue v on v.id = s.venue_id
group by v.city, c.state, v.name, v.id

SQLAlchemy 查询等价物:

query = Show.query.join(Venue)
.with_entities(Venue.city, Venue.state, Venue.name, Venue.id, func.count(Venue.id))
.group_by(Venue.city, Venue.state, Venue.name, Venue.id)

这只会是一个查询。所以我认为分组可以在应用程序站点完成:

results = {}
for city, state, name, id, show_count in query:
    location = (city, state)
    if location not in results:
        results[location] = []
    results[location].append({"id": id, "name": name, "num_upcoming_shows": show_count})
output = [
   {"city": key[0], "state": key[1], "venues": value for key, value in results.items()
]