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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:03:56+00:00 2026-06-03T13:03:56+00:00

I have a SyncEntities class (shown below). I have several other classes (such as

  • 0

I have a SyncEntities class (shown below).

I have several other classes (such as CommodityTypes also shown below) related to the SyncEntities class.

All of my Base subclasses have this column uuidKey = Column(String, primary_key=True)

Assume se is an instance of SyncEntities.
se.entityKind is the name of a Base subclass.

How do I query for an object that is in the se.entityKind class filtering for se.uuidKey?

class SyncEntities(Base):
    __tablename__ = 'SyncEntities'

    uuidKey = Column(String, primary_key=True)
    dateCreated = Column(DateTime, index=True)
    dateModified = Column(DateTime, index=True)
    dateSynced = Column(DateTime, index=True)
    username = Column(String)
    entityKind = Column(String)
    deleted = Column(Boolean)

    def __init__(self, entity, security):
        self.uuidKey = newUUID()
        self.dateCreated = security.now
        self.dateModified = security.now
        self.dateSynced = security.then
        self.username = security.username
        self.entityKind = entity.__tablename__
        self.deleted = False

    def modified(self, security):
        self.dateModified = security.now
        self.username = security.username

class CommodityTypes(Base):
    __tablename__ = 'CommodityTypes'
    uuidKey = Column(String, ForeignKey('SyncEntities.uuidKey'), primary_key=True)
    myName = Column(String, unique = True)
    sortKey = Column(Integer, unique = True)

    mySyncEntity = relationship("SyncEntities")

    def __init__(self, security, myName, sortKey):
        self.syncEntity = SyncEntities(self, security)
        self.uuidKey = self.syncEntity.uuidKey
        self.myName = myName
        self.sortKey = sortKey
  • 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-03T13:03:57+00:00Added an answer on June 3, 2026 at 1:03 pm

    The structure here is similar, though not quite the same, as a “polymorphic association”, and you can read about this pattern over at this blog post: http://techspot.zzzeek.org/2007/05/29/polymorphic-associations-with-sqlalchemy/ . It’s an old post but the example at http://techspot.zzzeek.org/files/2007/discriminator_on_association.py was added later as an updated example.

    This case is a little different in that an object like CommodityTypes only refers to a single SyncEntities, not multiple as in the usual polymorphic association. The SyncEntities also can only refer to a single type of related object since you have entityKind on it locally.

    I would note that a potential problem with this design is that you could have rows in other tables that have a uuidKey pointing to a particular SyncEntities instance, but are not of a type that matches “entityKind”. If the relationship between CommodityTypes and SyncEntities is actually one-to-one, that changes everything – this pattern is really simple joined table inheritance and you’d use the patterns described at http://docs.sqlalchemy.org/en/rel_0_7/orm/inheritance.html.

    You also don’t have backrefs between the target and SyncEntities, which is often a way to automate these styles of lookup. But you can still approximate things using a lookup of entityKind types to classes:

    def lookup_related(se):
        types = {
            'commodity':CommodityTypes,
            'foobar':FooBarTypes
        }
        cls = types[se.entityKind]
        session = object_session(se)
        return session.query(cls).filter(cls.mySyncEntity==se).all()
    

    here’s a mixin that could do it also, using a backref:

    class HasSyncEntity(object):
        entity_kind = None
        "subclasses need to populate this"
    
        @declared_attr
        def uuidKey(cls):
            return Column(String, ForeignKey("SyncEntities.uuidKey"), primary_key=True)
    
        @declared_attr
        def mySyncEntity(cls):
            return relationship("SyncEntities", backref="_%s_collection" % cls.entity_kind)
    

    CommodityTypes becomes:

    class CommodityTypes(HasSyncEntity, Base):
        entity_kind = "commodity"
        # ...
    

    You then add a method like this to SyncEntities, which looks up the appropriate backref, and you’re done:

    def get_related(self):
        return getattr(self, "_%s_collection" % self.entityKind)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Have written all the code in a silverlight class library (dll) and linked this
Have you tried to use SharePoint with version control such as Perforce (or Subversion),
How would you design an application (classes, interfaces in class library) in .NET when
Have a rather abstract question for you all. I'm looking at getting involved in
Have you ever had such an experience such as during a presentation something goes
Have an app that can use tts to read text messages. It can also
Have their been any studies related to the importance of how good a software
Have such a problem, hope you'll help me.. Can't find anywhere. Here is the
Have a bunch of classes which I need to do serialize and deserialize from/to
have next code: class GameTexture { private: LPDIRECT3DTEXTURE9 texture; unsigned char *alphaLayer; UINT width,

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.