English 中文(简体)
在声明表上形成许多对人的关系
原标题:SQLAlchemy many-to-many relationship on declarative tables

我有以下表格(非常简明的版本):

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

概况通过许多与人的关系与问题挂钩。 在连接表(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?

你在问世时提出的守则是正确的。 它只需要关于管理办公室一级关系的补充资料:

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

    ...

另外,you ben t setup Value forprofile_id and question_id in Yous. 回答功能,因为它使监察员办公室负责根据你的任务确定目标属性。

http://docs.sqlalchemy.org/en/latest/orm/extensions/declarative.html 阅读,可能也有助益。

问题回答

暂无回答




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...