Eine innere Verbindung mit SqlAlchemy
Ich habe eine rohe innere Verknüpfungsabfrage mit Zählung, die direkt in Postgres SQL geschrieben wurde:
SELECT "films"."id" AS "megaId",
COUNT("filmComments"."id") AS "numOfComments"
FROM "films"
INNER JOIN "filmComments"
ON ("films"."id" = "filmComments"."filmId")
GROUP BY "films"."id";
Wie kann ich das selbe mit normaler SqlAlchemy machen, ohneconnection.execute(sqlCode)
?
P.S. Meine SqlAlchemy-Tabellenklassen:
from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()
from sqlalchemy import Column, Integer, String, Date, Float
class Film(Base):
__tablename__ = "films"
id = Column(Integer, primary_key = True)
name = Column(String)
rating = Column(Float)
marksCount = Column(Integer)
commentsCount = Column(Integer, index=True)
class FilmComment(Base):
__tablename__ = "filmComments"
id = Column(Integer, primary_key = True)
filmId = Column(Integer, index=True)
rating = Column(Integer, index=True)
text = Column(String)
votesUp = Column(Integer)
votesDown = Column(Integer)
userId = Column(Integer)
date = Column(Date)