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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:47:02+00:00 2026-05-26T11:47:02+00:00

I have the following model (simplified): class User(Base): __tablename__ = ‘user’ id = Column(Integer,

  • 0

I have the following model (simplified):

class User(Base):
    __tablename__ = 'user'
    id = Column(Integer, primary_key=True)

class Thing(Base):
    __tablename__ = 'thing'
    id = Column(Integer, primary_key=True)

class Relationship(Base):
    __tablename__ = 'relationship'
    id = Column(Integer, primary_key=True)
    parent_id = Column(Integer, ForeignKey('thing.id'))
    parent = relationship('Thing', backref='parentrelationships', primaryjoin = "Relationship.parent_id == Thing.id")
    child_id = Column(Integer, ForeignKey('thing.id'))
    child = relationship('Thing', backref='childrelationships', primaryjoin = "Relationship.child_id == Thing.id")

class Vote(Base)
    __tablename__ = 'vote'
    id = Column(Integer, primary_key=True)
    rel_id = Column(Integer, ForeignKey('relationship.id'))
    rel = relationship('Relationship', backref='votes')
    voter_id = Column(Integer, ForeignKey('user.id'))
    voter = relationship('User', backref='votes')

I wanted to query all Relationships with a certain parent, and I also want to query votes made by a certain user on those Relationships. What I’ve tried:

def get_relationships(thisthing, thisuser):
    return DBSession.query(Relationship, Vote).\
        filter(Relationship.parent_id == thisthing.id).\
        outerjoin(Vote, Relationship.id == Vote.rel_id).\
        filter(Vote.voter_id == thisuser.id).\
        filter(Vote.rel_id == Relationship.id).\
        all()

as well as:

def get_relationships(thisthing, thisuser):
    session = DBSession()
    rels = session.query(Relationship).\
        filter(Relationship.parent_id == thisthing.id).\ 
        subquery()
    return session.query(rels, Vote).\
        outerjoin(Vote, rels.c.id == Vote.rel_id).\
        filter(Vote.voter_id == thisuser.id).\
        all()

I get nulls when I do either of these queries. What am I doing wrong?

  • 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-05-26T11:47:03+00:00Added an answer on May 26, 2026 at 11:47 am

    Just turn on SQL logging (echo=True) and you will see that the resulting SQL query for the first option is something like:

    SELECT relationship.id AS relationship_id, relationship.parent_id AS relationship_parent_id, relationship.child_id AS relationship_child_id, vote.id AS vote_id, vote.rel_id AS vote_rel_id, vote.voter_id AS vote_voter_id 
    FROM relationship LEFT OUTER JOIN vote ON relationship.id = vote.rel_id 
    WHERE relationship.parent_id = ? AND vote.voter_id = ? AND vote.rel_id = relationship.id
    

    If you examine it, you will notice that the clause vote.rel_id = relationship.id is part of both the JOIN clause and the WHERE clause, which makes the query to filter out those Relationship rows which do not have any votes by requested user.

    Solution:

    1. Remove redundant filter(Vote.rel_id == Relationship.id). part from the query.
    2. Edit-1: Also move (remove) the filter for the user filter(Vote.voter_id == thisuser.id) out of WHERE and into the LEFT JOIN clause: outerjoin(Vote, and_(Relationship.id == Vote.rel_id, Vote.voter_id == thisuser.id)).
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (heavily simplified) model, that uses will_paginate class Search < ActiveRecord::Base
I have the following model structure: class Container(models.Model): pass class Generic(models.Model): name = models.CharacterField(unique=True)
I have the following model: class User: name = models.CharField( max_length = 200 )
I have following model: class Customer < ActiveRecord::Base has_one :cart end class Cart <
I have the following model (simplified): abstract class CartItem { EntityReference<Cart> Cart; } class
I have the following model (greatly simplified for the purposes of this question): class
I have a following model: class Car(models.Model): make = models.CharField(max_length=40) mileage_limit = models.IntegerField() mileage
I have the following Model pattern: public abstract class PARENTCLASS {...} public class CHILD_A_CLASS
I have the following model and instance: class Bashable(models.Model): name = models.CharField(max_length=100) >>> foo
I have the following model setup - a User is interested in projects in

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.