Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869455
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:29:08+00:00 2026-06-14T17:29:08+00:00

I am trying to create a query which will return all shows in database

  • 0

I am trying to create a query which will return all shows in database ordered by number of users who have it as favorite.

Simplified working code:

from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.sql import func
import logging

app = Flask(__name__)
db = SQLAlchemy(app)

logging.basicConfig()
logging.getLogger('sqlalchemy.engine').setLevel(logging.INFO)

favorite_series = db.Table('favorite_series',
        db.Column('user_id', db.Integer, db.ForeignKey('users.id')),
        db.Column('series_id', db.Integer, db.ForeignKey('series.id')))

class User(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)
    favorite_series = db.relationship('Serie', secondary=favorite_series,
            backref=db.backref('users', lazy='dynamic'))

    def __repr__(self):
        return '<User {0}>'.format(self.name)


class Serie(db.Model):
    __tablename__ = 'series'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String)

    def __repr__(self):
        return '<Serie {0}>'.format(self.name)

u1 = User()
u1.name = 'user1'

u2 = User()
u2.name = 'user2'

u3 = User()
u3.name = 'user3'

s1 = Serie()
s1.name = 'Serie1'

s2 = Serie()
s2.name = 'Serie2'

s3 = Serie()
s3.name = 'Serie3'

s4 = Serie()
s4.name = 'Serie4'

s5 = Serie()
s5.name = 'Serie5'

u1.favorite_series.extend([s1, s3, s5])
u2.favorite_series.extend([s1, ])
u3.favorite_series.extend([s1, s2, s3])

u1.favorite_series.extend([s1, s2])
db.session.add(u1)
db.session.add(u2)
db.session.add(u3)
db.session.add(s1)
db.session.add(s2)
db.session.add(s3)
db.session.add(s4)
db.session.add(s5)
db.create_all()
db.session.commit()

And I try to retrieve them with:

shows = Serie.query.join(Serie.users).order_by(func.count(Serie.users)).all()

print shows

But this throws error in SQL syntax, I tried to search for something but could not come up with anything working.

Any help would be appreciated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:29:09+00:00Added an answer on June 14, 2026 at 5:29 pm

    Working solution:

    sub = db.session.query(favorite_series.c.series_id, func.count(favorite_series.c.user_id).label('count')).group_by(favorite_series.c‌​.series_id).subquery()
    shows = db.session.query(Serie, sub.c.count).outerjoin(sub, Serie.id == sub.c.series_id).order_by(db.desc('count')).all()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create a query which will return all rows of a
I'm trying to create a query that will return all the jobs published by
I'm trying to create a efficient query which will retrieve 'projects' from a database
Im trying to create a function, that will return a mysql query, which i
Im trying to create a select command to query the database I have and
I am trying to create a query which will check how many entries I
I have a query where I want to return all the rows which are
I'm trying to create a faster query, right now i have large databases. My
I have a web application which will return a user id based on the
I'm trying to write a query that will return data grouped by date ranges

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.