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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T15:15:46+00:00 2026-05-15T15:15:46+00:00

I have the following tables defined declaratively (very simplified version): class Profile(Base): __tablename__ =

  • 0

I have the following tables defined declaratively (very simplified version):

class Profile(Base):
        __tablename__ = 'profile'

        id = Column(Integer, primary_key = True)
        name = Column(String(65), nullable = False)

        def __init__(self, name):
            self.name = name


class Question(Base):
    __tablename__ = 'question'

    id = Column(Integer, primary_key = True)
    description = Column(String(255), nullable = False)
    number = Column(Integer, nullable = False, unique = True)


    def __init__(self, description, number):
        self.description = description
        self.number = number



class Answer(Base):
    __tablename__ = 'answer'

    profile_id = Column(Integer, ForeignKey('profile.id'), primary_key = True)
    question_id = Column(Integer, ForeignKey('question.id'), primary_key = True)
    value = Column(Integer, nullable = False)


    def __init__(self, profile_id, question_id, value):
        self.profile_id = profile_id
        self.question_id = question_id
        self.value = value

Profile is linked to Question via a many-to-many relationship. In the linking table (Answer) I need to store a value for the answer.

The documentation says I need to use an association object to do this but it’s confusing me and I can’t get it to work.

How do I define the many-to-many relationship for the Profile and Question tables using Answer as the intermediary table?

  • 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-15T15:15:46+00:00Added an answer on May 15, 2026 at 3:15 pm

    The documentation says I need to use
    an association object to do this but
    it’s confusing me and I can’t get it
    to work.

    That’s right. And the Answer class is your association object as it maps to the association table ‘answer’.

    How do I define the many-to-many
    relationship for the Profile and
    Question tables using Answer as the
    intermediary table?

    The code you’ve presented in your question is correct. It only needs additional information about relationships on the ORM level:

    from sqlalchemy.orm import relationship
    
    ...
    
    class Profile(Base):
        __tablename__ = 'profile'
    
        ...
    
        answers = relationship("Answer", backref="profile")
    
        ...
    
    
    class Question(Base):
        __tablename__ = 'question'
    
        ...
    
        answers = relationship("Answer", backref="question")
    
        ...
    

    Also, you shouldn’t setup values for profile_id and question_id in your Answer’s init function, because it’s the ORM that’s responsible for setting them accordingly based on you assignments to relationship attributes of your objects.

    You may be interested in reading documentation for declarative, especially the part about configuring relationships. Reading about working with related objects may be helpful as well.

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

Sidebar

Ask A Question

Stats

  • Questions 528k
  • Answers 528k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The ANSI INFORMATION_SCHEMA tables (in this case, INFORMATION_SCHEMA.COLUMNS) provide more… May 16, 2026 at 11:07 pm
  • Editorial Team
    Editorial Team added an answer This is up to you. Some sites put there mobile… May 16, 2026 at 11:07 pm
  • Editorial Team
    Editorial Team added an answer you missed one more parameter: post_max_size. its default is 8M... May 16, 2026 at 11:07 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

Hi have the following tables defined in my database: Transactions: - TransactionID (PK, Identity)
I have the following tables: Financial : PK_FinancialID FK_SchoolID School : PK_SchoolID Class :
Suppose i have the following 2 SQL tables: Foo Column DataType --------------------------- Title NVARCHAR(20)
I have the following tables in my database: Announcements: - AnnouncementID (PK) - Title
I have a legacy system where the relationship between 2 tables haven't been defined
In one of my MySQL tables, I have following columns: Skills varchar(80) Industry varchar(40)
This issue is driving me mad. I have several tables defined, and CRUD stored
I have the following tables for a competition: User: Id Name Email EntryId Entry:
I'm experimenting with the Doctrine ORM (v1.2) for PHP. I have defined a class
I have a table defined as: create table Foo ( X integer, Y timestamp,

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.