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

  • SEARCH
  • Home
  • 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 9095369
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T23:34:53+00:00 2026-06-16T23:34:53+00:00

i have users and entities (many-to-many) , and using sqlalchemy, with this model: from

  • 0

i have users and entities (many-to-many) , and using sqlalchemy, with this model:

from sqlalchemy import Table, Column, Unicode, Integer, ForeignKey
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base

from sqlalchemy.orm import scoped_session, sessionmaker

from zope.sqlalchemy import ZopeTransactionExtension

DBSession = scoped_session(sessionmaker(extension=ZopeTransactionExtension()))
Base = declarative_base()
users_entities = Table('users_entities', Base.metadata,
    Column('userID', Integer, ForeignKey('users.id')),
    Column('entitieID', Integer, ForeignKey('entities.id'))
)


class User(Base):
    __tablename__ = 'users'
    id = Column(Integer, primary_key=True)
    username = Column(Unicode(20))
    password = Column(Unicode(101))
    entities = relationship("Entities", secondary=users_entities)

    def __init__(self, username, password):
        self.username = username
        self.password = password


class Entities(Base):
    __tablename__ = 'entities'
    id = Column(Integer, primary_key=True)
    name = Column(Unicode(20))
    descr = Column(Unicode(101))
    url = Column(Unicode(101))

    def __init__(self, name, descr, url):
        self.name = name
        self.descr = descr
        self.url = url

so, when i use:

user = dbsession.query(User).filter_by(id=session["userID"]).first()
entities = user.entities

i get the user with the user data, and the entities(user.entities) with all the user’s entities.

but instead of getting all the entities, now i need to get an entity from the user where the id is = X
something that would work like this:

user.query(Entities).filter_by(id=X)

i can’t find a simple(“best”) way to do this, am i missing something?

  • 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-16T23:34:54+00:00Added an answer on June 16, 2026 at 11:34 pm

    You are looking for Dynamic Relationship Loaders, they’re exactly what you want to do, and enable you to write:

    user.entities.filter_by(id=X)
    

    By the way, you can write user = dbsession.query(User).get(session["userID"]). It does the same query, but only if the object is not already in the session cache (plus, it’s shorter).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two entities, a User and Role with a one-to-many relationship from user
I have two entities, projects and users. These are modeled in Rails using Mongoid
I have a one to many relationship between accounts and users. I am using
I have an admin form that lets users create entities that require an image.
I have several entities which respresent different types of users who need to be
I have a GWT (+GAE) webapp that allows users to edit Customer entities. When
I have two entities Customer and Order in a one-to-many relation. For each customer
I have created a very simple Entity Framework 4.0 model using VS2010 Professional. Ignore
I have this problem where I have a one-to-many relationship and I have to
I am using Code First, Entities, MVC3 + Razor, we have forms which have

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.