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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:43:20+00:00 2026-05-23T03:43:20+00:00

Is it possible to specify a discriminator column from another table? How do I

  • 0

Is it possible to specify a discriminator column from another table? How do I do this with Declarative?

The reason for this is I have a joined table inheritance with

class User(Base):
    id = Column(...)

class Customer(User):
    customer_id = Column('id', ...)

class Mechanic(User):
    mechanic_id = Column('id', ...)

class Role(Base):
    id = Column(..)

but want to discriminate by Roles a user has. A user could have a buyer role or a seller role or both.

Is this the correct way to model this scenario?

As a note, there is Buyer specific data and Seller specific data as well, and that is why I am using the joined table inheritance.

  • 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-23T03:43:21+00:00Added an answer on May 23, 2026 at 3:43 am

    Here’s an awkward and inefficient way to achieve the specific result, which will also fail if more than one Role is present on the target User:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    role_to_user = Table('role_to_user', Base.metadata,
        Column('role_id', Integer, ForeignKey('role.id'), primary_key=True),
        Column('user_id', Integer, ForeignKey('user.id'), primary_key=True),
    )
    
    class Role(Base):
        __tablename__ = 'role'
        id = Column(Integer, primary_key=True)
        name = Column(String, nullable=False)
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
    
        discrim = select([Role.name])
        discrim_col = discrim.label("foo")
        type = column_property(discrim_col)
        roles = relationship("Role", secondary=role_to_user)
        __mapper_args__ = {'polymorphic_on': discrim_col}
    
    User.discrim.append_whereclause(Role.id==role_to_user.c.role_id)
    User.discrim.append_whereclause(role_to_user.c.user_id==User.id)
    
    class Customer(User):
        __tablename__ = 'customer'
        id = Column(Integer, ForeignKey(User.id), primary_key=True)
        __mapper_args__ = {'polymorphic_identity':'customer'}
    
    class Mechanic(User):
        __tablename__ = 'mechanic'
        id = Column(Integer, ForeignKey(User.id), primary_key=True)
    
        __mapper_args__ = {'polymorphic_identity':'mechanic'}
    
    e = create_engine('sqlite://', echo=True)
    
    Base.metadata.create_all(e)
    
    s = Session(e)
    
    m, c = Role(name='mechanic'), Role(name='customer')
    s.add_all([m, c])
    s.add_all([Mechanic(roles=[m]), Customer(roles=[c])])
    s.commit()
    
    print Session(e).query(User).all()
    

    Next, here is how I would actually do it, if I had the issue of assigning behaviors based on zero or more Roles – I’d put the behavior in the Role, not the owner of the Role:

    from sqlalchemy import *
    from sqlalchemy.orm import *
    from sqlalchemy.ext.declarative import declarative_base
    
    Base = declarative_base()
    
    class User(Base):
        __tablename__ = 'user'
        id = Column(Integer, primary_key=True)
        name = Column(String)
        roles = relationship("Role", secondary= Table(
                        'role_to_user', Base.metadata,
                        Column('role_id', Integer, 
                                ForeignKey('role.id'), 
                                primary_key=True),
                        Column('user_id', Integer, 
                                ForeignKey('user.id'), 
                                primary_key=True),
                        ),
                        lazy="subquery"
                    )
    
        def operate_on_roles(self):
            for role in self.roles:
                role.operate(self)
    
    class Role(Base):
        __tablename__ = 'role'
        id = Column(Integer, primary_key=True)
        type = Column(String, nullable=False)
        __mapper_args__ = {'polymorphic_on': type}
    
    class CustomerRole(Role):
        __tablename__ = 'customer'
        id = Column(Integer, ForeignKey(Role.id), primary_key=True)
        __mapper_args__ = {'polymorphic_identity':'customer'}
    
        def operate(self, user):
            print "user %s getting my car fixed!" % user.name
    
    class MechanicRole(Role):
        __tablename__ = 'mechanic'
        id = Column(Integer, ForeignKey(Role.id), primary_key=True)
        __mapper_args__ = {'polymorphic_identity':'mechanic'}
    
        def operate(self, user):
            print "user %s fixing cars!" % user.name
    
    e = create_engine('sqlite://', echo=True)
    
    Base.metadata.create_all(e)
    
    s = Session(e)
    
    m, c = MechanicRole(), CustomerRole()
    s.add_all([m, c])
    s.add_all([
            User(name='u1', roles=[m, c]),
            User(name='u2', roles=[c]),
            User(name='u3', roles=[m]),
        ])
    s.commit()
    
    for user in s.query(User):
        user.operate_on_roles()
    

    The second example produces the output:

    user u1 fixing cars!
    user u1 getting my car fixed!
    user u2 getting my car fixed!
    user u3 fixing cars!
    

    As you can see the second example is clear and efficient while the first is just a thought experiment.

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

Sidebar

Related Questions

Possible Duplicate: Specify Command for MenuItem in a DataTemplate I have a collection of
Is it possible to specify a different image when the user's focus comes to
Is it possible to specify a Java classpath that includes a JAR file contained
Is it possible to specify my own default object instead of it being null?
Is it possible to specify a relative connection string for an AzMan XML store?
Am I doing something wrong or is it not possible to specify a generic
In VS2005 and up, is it possible to specify which configuration should be selected
Possible Duplicate: How do I calculate someone's age in C#? Maybe this could be
Is it possible to specify which X display the JVM is to launch it's
Is it possible to specify the JVM to use when you call java jar

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.