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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T09:57:38+00:00 2026-05-29T09:57:38+00:00

There is an info table and it has relationship to car table or suv

  • 0

There is an info table and it has relationship to car table or suvtable.

Itis specified in info.type field.

So how can I create association on fly based on the type data of the record?

class Info(Base):
    item_id = Column(ForeignKey('cars-or-suvs-table.id'))
    type = Column(String())

class Car(Base):
    - data - 

    info = relationship('Info', backref="car")

class Suv(Base):
    - data - 

    info = relationship('Info', backref="suv")

Edit: I already have the tables filled with data, so I can not change db schema.

  • 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-29T09:57:38+00:00Added an answer on May 29, 2026 at 9:57 am

    Since you’re looking for a solution that doesn’t require moving the foreign key to a different table, you can try this approach:

    import sqlalchemy
    from sqlalchemy.ext.declarative import declarative_base
    from sqlalchemy import Column, String, Integer
    from sqlalchemy.orm import sessionmaker, relationship
    from sqlalchemy import sql
    Base = declarative_base()
    engine = sqlalchemy.create_engine('sqlite:///:memory:')
    Session = sessionmaker(bind=engine)
    session = Session()
    
    class Info(Base):
        __tablename__ = 'info'
        id = Column(Integer(), primary_key=True)
        type = Column(String())
        item_id = Column(Integer())
    
        @property
        def item(self):
            if self.type == 'car':
                return self._car
            elif self.type == 'suv':
                return self._suv
            return None
    
        @item.setter
        def item(self, value):
            if value is not None:
                self.item_id = value.id
                self.type = value.__tablename__
            else:
                self.item_id = None
    
    class Car(Base):
        __tablename__ = 'car'
        id = Column(Integer(), primary_key=True)
        info = relationship(Info, primaryjoin=sql.and_(id == Info.item_id, Info.type == 'car'), foreign_keys=Info.item_id, uselist=False, backref='_car')
    
    class Suv(Base):
        __tablename__ = 'suv'
        id = Column(Integer(), primary_key=True)
        info = relationship(Info, primaryjoin=sql.and_(id == Info.item_id, Info.type == 'suv'), foreign_keys=Info.item_id, uselist=False, backref='_suv')
    

    I renamed Info.car to Info._car since ._car will unavoidably be a bogus car object even if .type is ‘suv’.

    I’ve left the event listener stuff out to keep it simple, but you can definitely adapt what pieces you need from my other answer to avoid things getting into an inconsistent state.

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

Sidebar

Related Questions

Is there any possibility in Yii to add more info in yiisession table. Be
I have a table that has a relationship between an User_ID and a Role.
Hello I have a table has some info like this id | message 1
Like most apps, mine has a users table that describes the entities that can
There are two tables. One is users info users, one is comments info comments.
Any info out there on how to print the generated charts in say PDF
There doesn't seem to be much info on this topic so I'm going to
Is there a way to get the session info from a remote windows server
Originally there was the DAL object which my BO's called for info and then
Is there any method / API defined to collect system info in osx. I

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.