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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:55:23+00:00 2026-06-01T07:55:23+00:00

I have three tables which are defined as: class User(Base): __tablename__ = ‘users’ id

  • 0

I have three tables which are defined as:

class User(Base):
    __tablename__ = 'users'

    id = Column(Integer(10), primary_key=True)
    firstname = Column(String(64))
    surname = Column(String(64))

class SWMS(Base):
    __tablename__ = 'swms'
    id = Column(Integer(10), primary_key=True)
    owner_id = Column(Integer(10), ForeignKey('users.id', ondelete='CASCADE'))
    filename = Column(String(255))

    swmowner = relationship('User', backref=backref('users'))

class SWM_perms(Base):
    __tablename__ = 'swm_perms'
    id = Column(Integer(10), primary_key=True)
    swm_id = Column(Integer(10), ForeignKey('swms.id', ondelete='CASCADE'))
    user_id = Column(Integer(10), ForeignKey('users.id', ondelete='CASCADE'))

    swm = relationship('SWMS', backref=backref('swms'))
    swmuser = relationship('User', backref=backref('swmusers'))

Essentially, the SWMS table is a table of document info where the owner_id defines the user who created the document. SWM_perms is a table that has a mapping of document id to user id – to define which users are allowed to see the document.

To produce a table of all documents which are either 1) owned by the user or 2) are viewable by the user, in SQL I would do:

select owner_id, users.firstname, users.surname, filename
from swms, swm_perms, users
where users.id=swms.owner_id and 
  ((swms.id=swm_perms.swm_id and swm_perms.user_id = 27) or (owner_id = 27));

How would you define this query in sqlalchemy? I am familiar with the or_() function but the variants I am trying do not generate the correct objects.

  • 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-01T07:55:25+00:00Added an answer on June 1, 2026 at 7:55 am
    cond1 = and_(SWMS.id==SWM_perms.swm_id,SWM_perms.user_id==27)
    swms = DBSession.query(User,SWMS).filter(or_(cond1,SWMS.owner_id==27)).\
                                      filter(User.id==SWMS.owner_id).all()
    

    and then you can do a list comprehension to pull the fields you want:

    details = [(u.firstname, s.filename, s.blob_key, s.last_modified) for u,s in swms]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three tables of concern and a query which generates the following output
I have a table which needs to link one of three seperate tables, but
I have a table in HTML which has three columns i.e Error - User
I have three tables that I am working with User, Application, ApplicationAdministrator. ApplicationAdministrator is
My situation I have an entity model with a base class ItemBase which defines
I have an existing database with tables which each have 4 partitions (there are
I have two tables which have some transactional stuff stored in them. There will
I have a SqlServer 2008 table which has a Primary Key (IsIdentity=Yes) and three
I have a table PGM_MASTER, which has three columns PGM_ID | int(11) PGM_ENV_ID |
I have an abstract class defined as follows: abstract class Abstract Parent extends Zend_Db_Table_Abstract

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.