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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:17:30+00:00 2026-06-17T14:17:30+00:00

Below there is a pseudo Many-to-Many table structure of say a customer ordering an

  • 0

Below there is a pseudo Many-to-Many table structure of say a customer ordering an event. What I would ideally like to be able to achieve is do implicit queries and sets by using the different relationship names.

For example (presume that the init code creates the associated objects)

order=order()
order.events.corporate.name = "Big Corp"

This would set the event.type (enum) to “corporate” and the event.name to “Big Corp”. Similarly.

print session.query(Order).filter(order.events.corporate.name == "Big Corp")

This would only find the event record that had the associated event.type = “corporate”. (Trivial example and not necessary but you get the idea)

Similarly using order.events.personal.name would set/query the corresponding records with event.type = “personal”

Your help in understanding what is the best way of achieving this functionality would be much appreciated.

Base = declarative_base()

class Order(Base):
    __tablename__ = 'order'
    id                  = Column(Integer, primary_key=True)
    event_id            = Column(Integer, ForeignKey('events.id'))

    events = relationship("Events")

class Events(Base):
    __tablename__ = 'events'
    order_id            = Column(Integer, ForeignKey('order.id'), primary_key=True)
    event_id            = Column(Integer, ForeignKey('event.id'), primary_key=True)

    corporate = relationship("Event")
    personal  = relationship("Event")
    event     = relationship("Event")

class Event(Base):
    __tablename__ = 'event'
    id                  = Column(Integer, primary_key=True)
    type                = Column(Enum('corporate', 'personal', name='enum_ev_type'))
    name                = Column(String(32))
  • 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-17T14:17:31+00:00Added an answer on June 17, 2026 at 2:17 pm

    SQLAlchemy do not understand the case when relationship is used in filter. In your example order.events.corporate.

    I’ve got the following exception while trying to do so:

    AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object associated with Order.events has an attribute 'corporate'
    

    I would suggest to consider using AssociationObject pattern described in SQL Alchemy Relationships documentation page.

    So the query would be:

    session.query(Order).filter(and_(EventsAssoc.type=="corporate",Event.name=="Big Corporation"))
    

    See full example how to define schema and create objects.

    from sqlalchemy import *
    from sqlalchemy import create_engine, orm
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy.orm import relationship
    
    
    
    metadata = MetaData()
    Base = declarative_base()
    Base.metadata = metadata
    
    
    
    class Event(Base):
        __tablename__ = 'event'
        id                  = Column(Integer, Sequence("event_seq"), primary_key=True)
        name                = Column(String(32))
        def __repr__(self):
            return "%s(name=\"%s\",id=\"%s\")" % (self.__class__.__name__,self.name,self.id)
    
    
    class EventsAssoc(Base):
        __tablename__ = 'events'
        id                  = Column(Integer, Sequence("events_seq"), primary_key=True)
        left_id = Column(Integer, ForeignKey('order.id'))
        right_id = Column(Integer, ForeignKey('event.id'))
    
    #    order_id            = Column(Integer, ForeignKey('order.id'), primary_key=True)
    #    event_id            = Column(Integer, ForeignKey('event.id'), primary_key=True)
        type                 = Column(Enum('corporate', 'personal', name='enum_ev_type'))
    
        event = relationship(Event, backref="order_assocs")
    
        def __repr__(self):
            return "%s(events=%r,id=\"%s\")" % (self.__class__.__name__,self.event,self.id)
    
    
    class Order(Base):
        __tablename__ = 'order'
        id                  = Column(Integer, Sequence("order_seq"), primary_key=True)
        name = Column(String(127))
        events = relationship(EventsAssoc)
        def __repr__(self):
            return "%s(name=\"%s\",id=\"%s\")" % (self.__class__.__name__,self.name,self.id)
    
    
    db = create_engine('sqlite:////temp/test_assoc.db',echo=True)
    
    
    
    #making sure we are working with a fresh database
    metadata.drop_all(db)
    metadata.create_all(db)
    
    
    sm = orm.sessionmaker(bind=db, autoflush=True, autocommit=True, expire_on_commit=True)
    session = orm.scoped_session(sm)
    
    o = Order(name="order1")
    ea_corp = EventsAssoc(type="corporate")
    ea_corp.event = Event(name="Big Corporation")
    
    ea_pers = EventsAssoc(type="personal")
    ea_pers.event = Event(name="Person")
    
    
    o.events.append(ea_corp)
    o.events.append(ea_pers)
    session.add(o)
    session.flush()
    
    query = session.query(Order).filter(and_(EventsAssoc.type=="corporate",Event.name=="Big Corporation"))
    
    for order in query.all():
        print order
        print order.events
    

    Here is the query produced by sqlalchemy:

    SELECT "order".id AS order_id, "order".name AS order_name 
    FROM "order", events, event 
    WHERE events.type = ? AND event.name = ?
    ('corporate', 'Big Corporation')
    

    PS To enhance the association object pattern such that direct access to the EventsAssoc object is optional, SQLAlchemy provides the Association Proxy extension

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

Sidebar

Related Questions

In my web.config file there are below entries. It seems like it is used
Is there any difference between the below 2 CREATE TABLE statements in SQL Server
Hi there below is the pseudo code for my binary search implementation: Input: (A[0...n-1],
Within a C++ JNI function (semi-pseudo included below), there is at least one (and
In the Code Below there are Two Classes. One Object of type two is
In the image below there is an area, which has an unknown (custom) class.
See edits below There is no casting going on with the termination check. I
In below link there is add button which allows you to ADD multiple textbox
For a json data as below is there any possible way to construct a
If I declare a variable within a block (see below) is there a way

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.