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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T00:35:44+00:00 2026-06-02T00:35:44+00:00

If I have two tables: A (id, … some other columns) and B (id,

  • 0

If I have two tables: A (id, … some other columns) and B (id, … some other columns). I need to associate Comments (another table: id, text, author) with A and B objects.

Can I do something like creating the fourth table for that purpose: comment_id, table_id (A or B or maybe others), item_id? I mean some sqlalchemy way?

Now I know only of those two solutions:
http://bpaste.net/show/27149/ – for each A and B there will be separate table with comments. I don’t think it’s a good idea, because those tables are (should be) identical, if I want to see all comments by some author – it will be more difficult than the other ways, if there will be C table someday – I will need to create a table for its comments…

Another solution – http://bpaste.net/show/27148/. I think is better, but I still will be needing to create an association table for every table I need to comment the items.

Any ideas? Thanks in advance.

  • 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-02T00:35:46+00:00Added an answer on June 2, 2026 at 12:35 am

    You could try:

    class Comment(Base):
        __tablename__ = 'comments'
    
        id = Column(Integer, primary_key=True)
        a_id = Column(Integer, ForeignKey('a.id'), nullable=True)
        b_id = Column(Integer, ForeignKey('b.id'), nullable=True)
        text = Column(UnicodeText, nullable=False)
    
        def __init__(self, text):
            self.text = text
    

    It is still a relatively hacky way to do it, but it preserves referential integrity by using foreign keys.

    If you’d like to go for the solution where you store the table name, you could try something like this:

    class A(Base):
        __tablename__ = 'as'
    
        id = Column(Integer, primary_key=True)
        name = Column(Unicode, nullable=False, unique=True)
        comments = relationship("Comment",
                        primaryjoin="and_(A.id==Comment.id, "
                            "Comment.model=='A')")
    
        def __init__(self, name):
            self.name = name
    
    class Comment(Base):
        __tablename__ = 'comments'
    
        id = Column(Integer, primary_key=True)
        text = Column(UnicodeText, nullable=False)
        model = Column(String(50), nullable=False)
    
        def __init__(self, text):
            self.text = text
    

    I have not tested this myself, so if you run into problems have a look at Specifying Alternate Join Conditions or comment and I will do more research.

    For an in-depth explanation of the subject, see Mike Bayer’s blog entry on Polymorphic Associations

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

Sidebar

Related Questions

I have two tables. One table contains information about Assets, another Table about their
I have two tables. One is temp table and another is main table. In
I have two tables, orders and old_orders. Each table has some similar fields and
I have two tables Table : 1, Name : NdcAwp, Columns-Ndc, AwpUnitCost Table :
I have two tables. threads with the columns ID, forumID, title, content, unixtime comments
I have two tables. table A id isReachEnd table B id isFree I need
I have two tables : teachers (teacher_id,teacher_name) courses (teacher_id,course_id) And I need to display
I have two tables, for example: Table A Table B ======= ======= Name |
I have two tables: a schedule table that contains information about how an employee
I have two tables with this structure: Table one: ID Description Table two: ID

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.