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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:32:17+00:00 2026-05-27T01:32:17+00:00

Please help me model this in SQLAlchemy. A user can create a question. A

  • 0

Please help me model this in SQLAlchemy. A user can create a question. A question can have any number of choices (E.g. YES, NO, LATER, MAY BE, DONT KNOW, NEXT YEAR, NOT APPLICABLE). I have created a mapper between questions and choices. How do I model the responses in SQLAlchemy?

question_choices = Table('question_choices', Base.metadata,
    Column('id', Integer, primary_key=True),
    Column('question_id', Integer, ForeignKey('questions.id')),
    Column('choice_id', Integer, ForeignKey('choices.id'))
    )

class Choice(Base):
    __tablename__ = 'choices'
    id = Column(Integer, primary_key=True)
    value = Column(String(30), nullable=False)

class Question(Base):
    __tablename__ = 'questions'
    id = Column(Integer, primary_key=True)
    title   = Column(String(100))
    created = Column(DateTime)

    choices = relationship('Choice', secondary=question_choices)

class questionResponse(Base):
    """A friend's response to a question"""
    __tablename__ = 'question_responses'
    id = Column(Integer, primary_key=True)
    question_id = Column(Integer, ForeignKey('questions.id'))
    choice_id = Column(Integer, ForeignKey('choices.id'))
    user_id = Column(Integer, ForeignKey('users.id'))
    created = Column(DateTime)

The questionResponse model is not normalized. Question_id and listing_id are repeated. I do not have a relationship in the mapper table. I want to be able to count the responses for a given question.

  • 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-27T01:32:18+00:00Added an answer on May 27, 2026 at 1:32 am

    Your mapper for QuestionResponse is pretty good already, but it does not restrict an answer of the choice that is not configured: so if LATER is not allowed answer for a question Will you marry me?, the database does not restrict this.

    One solution to this would be to also add a two-column foreign key constraint to the QuestionResponse:

    class QuestionResponse(Base):
        """A friend's response to a question"""
        __tablename__ = 'question_responses'
        id = Column(Integer, primary_key=True)
        question_id = Column(Integer, ForeignKey('questions.id'))
        choice_id = Column(Integer, ForeignKey('choices.id'))
        # ...
        __table_args__ = (
                ForeignKeyConstraint(['question_id', 'choice_id'], ['question_choices.question_id', 'question_choices.choice_id']),
                )
    

    Alternative (more normalized DB model) is to define the FK only to the question_choices.id:

    class QuestionResponse(Base):
        """A friend's response to a question"""
        __tablename__ = 'question_responses'
        id = Column(Integer, primary_key=True)
        question_choice_id = Column(Integer, ForeignKey('question_choices.id'))
    

    edit-1: In this case you can have a relationship between Question and QuestionResponse defined like below, which will provide you with count as well:

    class Question(Base):
        # ....
        answers = relationship("QuestionResponse", 
            primaryjoin="Question.id==question_choices.c.question_id",
            secondary=question_choices,
            secondaryjoin="question_choices.c.id==QuestionResponse.question_choice_id",
            backref="question",
            )
    

    In any case you might want to add a UniqueConstraint to the question_choices table on columns (question_id, choice_id).


    Now, in order to count responses, you either add the relationship between Question and QuestionResponse and return len(answers) or you just create a query-based property on Question:

    class Question(Base):
        # ...
        answer_count = column_property(
                    select([func.count(QuestionResponse.__table__.c.id)]).
                    where(question_choices.c.question_id==id).
                    where(question_choices.c.id==QuestionResponse.__table__.c.question_choice_id)
                )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a bit of a trouble here, please help me. I have this
please help me solve this issue. I have a client using WCF. I don't
Please help me figure this out. I apologize if this is a duplicate question,
Can someone please help me with this conditional field validation in CodeIgniter? Trying to
I have been frustrated by this for like a day...please help. I have an
Please help me understand if the following choices I have made are idiomatic/good and
Please help! Background info I have a WPF application which accesses a SQL Server
Please help me convert this line to C#. objManagementBaseObject.SetPropertyValue(hDefKey, CType(&H & Hex(RegistryHive.LocalMachine), Long)) Related
Please help me with using the DrScheme built-in function filter. create a function hello
dear all..i have this data inside DB: line model serial fa01 kd-g335ud 105x0001 fa01

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.