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 8864155
In Process

The Archive Base Latest Questions

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

I have an SQLAlchemy scheme that looks roughly like this: participation = db.Table(‘participation’, db.Column(‘artist_id’,

  • 0

I have an SQLAlchemy scheme that looks roughly like this:

participation = db.Table('participation',
        db.Column('artist_id', db.Integer, db.ForeignKey('artist.id'),
                  primary_key=True),
        db.Column('song_id', db.Integer, db.ForeignKey('song.id'),
                  primary_key=True),
)

class Streamable(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    kind = db.Column(db.String(10), nullable=False)
    score = db.Column(db.Integer, nullable=False)
    __mapper_args__ = {'polymorphic_on': kind}

class Artist(Streamable):
    id = db.Column(db.Integer, db.ForeignKey('streamable.id'), primary_key=True)
    name = db.Column(db.Unicode(128), nullable=False)
    __mapper_args__ = {'polymorphic_identity': 'artist'}

class Song(Streamable):
    id = db.Column(db.Integer, db.ForeignKey('streamable.id'), primary_key=True)
    name = db.Column(db.Unicode(128), nullable=False)
    artists = db.relationship("Artist", secondary=participation,
                              backref=db.backref('songs'))
    __mapper_args__ = {'polymorphic_identity': 'song'}

class Video(Streamable):
    id = db.Column(db.Integer, db.ForeignKey('streamable.id'), primary_key=True)
    song_id = db.Column(db.Integer, db.ForeignKey('song.id'), nullable=False)
    song = db.relationship('Song', backref=db.backref('videos', lazy='dynamic'),
                           primaryjoin="Song.id==Video.song_id")
    __mapper_args__ = {'polymorphic_identity': 'video'}

I’d like to do a single query for Songs or Videos that have a particular artist; i.e., these two queries in one query (all queries should be .order_by(Streamable.score)):

q1=Streamable.query.with_polymorphic(Video)
q1.join(Video.song, participation, Artist).filter(Artist.id==1)
q2=Streamable.query.with_polymorphic(Song)
q2.join(participation, Artist).filter(Artist.id==1)

Here’s the best I reached; it emits monstrous SQL and always yields empty results (not sure why):

p1=db.aliased(participation)
p2=db.aliased(participation)
a1=db.aliased(Artist)
a2=db.aliased(Artist)
q=Streamable.query.with_polymorphic((Video, Song))
q=q.join(p1, a1).join(Video.song, p2, a2)
q.filter(db.or_((a1.id==1), (a2.id==1))).order_by('score')

What’s the right way to do this query, if at all (maybe a relational datastore is not the right tool for my job…)?

  • 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-14T16:10:09+00:00Added an answer on June 14, 2026 at 4:10 pm

    Your queries are basically right. I think the change from join to outerjoin should solve the problem:

    q=q.outerjoin(p1, a1).outerjoin(Video.song, p2, a2)
    

    I would also replace the order_by with:

    q = q.order_by(Streamable.score)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SqlAlchemy table like so: table = sql.Table('treeItems', META, sql.Column('id', sql.Integer(), primary_key=True),
I have a SQL query that I execute like this with an SQLAlchemy engine:
I have a SQLAlchemy model like this: class Group(Base): __tablename__ = 'groups' id =
Say I have a SqlAlchemy model something like this: from sqlalchemy.ext.declarative import declarative_base from
I have an sqlalchemy query that returns a tuple. I pass this tuple to
I have a declarative SQLAlchemy object with deferred columns, declared like this: class Review(Base):
I have the following model classes declared by SQLAlchemy: class User(Base): id = Column(Integer,
I have an SQLAlchemy model like the one below, and at first it didn't
I'm going to have two independent programs (using SqlAlchemy / ORM / Declarative) that
I got a simple problem in SQLAlchemy. I have one model in a table,

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.