I am still learning flask sqlalchemy but am working on a dashboard using flask, mysql workbench, and sqlalchemy. I would like to query two tables:
class User(db.Model):
id = Column(Integer(), primary_key=True)
name = Column(String(200))
first_name = Column(String(200))
class Certificates(db.Model):
id = Column(Integer, primary_key=True)
user_id = Column(Integer, ForeignKey("user.id",
ondelete="CASCADE"))
report_sent = Column(Boolean, default=False)
delivery_time = Column(DateTime)
certificate_type = Column(String(32))
I would like to know the total number of users that has a certificate
I tried:
user_with_cert = db.session.query(func.count(User.id)).filter(Certificates.certificate_type != None).filter(Certificates.user_id == User.id).all()
The query above returned the total number of records in the Certificate table not the actual number of users with a certificate.
I also tried:
user_cert = (
db.session.query(User, Certificates)
.join(Certificates)
.all()
)
for user, cert in b2c_cert:
t = db.session.query(func.count(user.id)).filter(
cert.user_id == user.id).filter(cert.certificate_type != None).all()
print(t)
Please how can I achieve this goal?