English 中文(简体)
缩略语
原标题:SqlAlchemy many to many data modeling

请帮助我把这一模式用在Lucy。 用户可以提出问题。 问题可以有任何选择(例如,)。 YES, NO, LATER, MAY BE, DONT KNOW, NEXT YEAR, NOT AppLICABLE. 我在问题与选择之间绘制了地图。 我如何模仿“结构”中的答复?

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)

www.un.org/Depts/DGACM/index_spanish.htm 问题解决模式没有正常化。 重复出现问题。 我在地图表中没有关系。 我想对一个问题做出答复。

最佳回答

www.un.org/Depts/DGACM/index_chinese.htm 您是否与我结婚?,数据库没有限制这一点。

www.un.org/Depts/DGACM/index_spanish.htm 其中一项解决办法是在<代码>上添加一条两栏外国关键制约因素。 问题Response:

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 ))

<<>strong>edit-1:> 在此情况下,你可以在下文所界定的问题与问题答复之间建立联系,这也将为你提供依据:

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",
        )

无论如何,您不妨在<代码>上添加一个<代码>UniqueConstraint。


现在,为了计算答复,你要么在<条码> 问 题/代码>和<条码> 问答题<>/代码>和返回<条码>/(回答)<条/代码>之间增加关系,要么在<条码>上设定一个基于质的财产。 问题:

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)
            )
问题回答

暂无回答




相关问题
sqlalchemy does not create my foreign key

SqlAlchemy newbie question: Base = declarative_base() class A(Base): __tablename__ = as id = Column(Integer, primary_key=True) class B(Base): __tablename__ = bs id = Column(...

How to get sqlalchemy length of a string column

Consider this simple table definition (using SQLAlchemy-0.5.6) from sqlalchemy import * db = create_engine( sqlite:///tutorial.db ) db.echo = False # Try changing this to True and see what happens ...

Can SQLAlchemy update the table structure?

I am working on my first pylons + SQLAlchemy app (I m new to both). As I change my mind on the table structure, I wish there was a similar function to metadata.create_all(), that checks if there are ...

SQLAlchemy 0.5.5 - defining table schema

I used sqlautocode to generate a model.py for an existing MySQL database and here s a table example: fusion_articles = Table( fusion_articles , metadata, Column(u article_id , Integer(), ...

热门标签