SQLAlchemy使用关联表

时间:2015-12-30 19:17:51

标签: python flask sqlalchemy many-to-many associations

我已经看到了一些与此类似的问题,但没有一个问题在头上发现。基本上,我在使用SQLAlchemy的Flask应用程序中有三个表格模型Center()Business()CenterBusiness()。目前我正以这种方式加入这种关系:

biz = Business(typId=form.type.data, name=form.name.data,
               contact=form.contact.data, phone=form.phone.data)
db.session.add(biz)
db.session.commit()

assoc = CenterBusiness(bizId=biz.id, cenId=session['center'])
db.session.add(assoc)
db.session.commit()

正如你所看到的那样有点难看,我知道有一种方法可以通过关系来实现它,因为它们已被定义。我在SQLAlchemy的文档中看到他们对使用这样的表有一个解释,但我似乎无法让它工作。

#Directly from SQLAlchemy Docs
p = Parent()
a = Association(extra_data="some data")
a.child = Child()
p.children.append(a)

#My Version Using my Tables
center = Center.query.get(session['center']
assoc = CenterBusiness()
assoc.business = Business(typId=form.type.data, name=form.name.data,
                          contact=form.contact.data, phone=form.phone.data)
center.businesses.append(assoc)
db.session.commit()

不幸的是,这似乎没有做到这一点......任何帮助都会非常感激,下面我发布了所涉及的模型。

class Center(db.Model):
    id = db.Column(MEDIUMINT(8, unsigned=True), primary_key=True,
                   autoincrement=False)
    phone = db.Column(VARCHAR(10), nullable=False)
    location = db.Column(VARCHAR(255), nullable=False)
    businesses = db.relationship('CenterBusiness', lazy='dynamic')
    employees = db.relationship('CenterEmployee', lazy='dynamic')

class Business(db.Model):
    id = db.Column(MEDIUMINT(8, unsigned=True), primary_key=True,
                   autoincrement=True)
    typId = db.Column(TINYINT(2, unsigned=True),
                      db.ForeignKey('biz_type.id',
                                    onupdate='RESTRICT',
                                    ondelete='RESTRICT'),
                      nullable=False)
    type = db.relationship('BizType', backref='businesses',
                           lazy='subquery')
    name = db.Column(VARCHAR(255), nullable=False)
    contact = db.Column(VARCHAR(255), nullable=False)
    phone = db.Column(VARCHAR(10), nullable=False)
    documents = db.relationship('Document', backref='business',
                                lazy='dynamic')

class CenterBusiness(db.Model):
    cenId = db.Column(MEDIUMINT(8, unsigned=True),
                      db.ForeignKey('center.id',
                                    onupdate='RESTRICT',
                                    ondelete='RESTRICT'),
                      primary_key=True)
    bizId = db.Column(MEDIUMINT(8, unsigned=True),
                      db.ForeignKey('business.id',
                                    onupdate='RESTRICT',
                                    ondelete='RESTRICT'),
                      primary_key=True)
    info = db.relationship('Business', backref='centers',
                           lazy='joined')
    archived = db.Column(TINYINT(1, unsigned=True), nullable=False,
                         server_default='0')

2 个答案:

答案 0 :(得分:1)

我能够让这个工作,我的问题在下面的代码中出现(粗体错误):

#My Version Using my Tables
center = Center.query.get(session['center']
assoc = CenterBusiness()
**assoc.info** = Business(typId=form.type.data, name=form.name.data,
                          contact=form.contact.data, phone=form.phone.data)
center.businesses.append(assoc)
db.session.commit()

正如我在问题中的评论中所解释的那样:

  

好吧我的问题是我没有使用关系键“info”   我在我的CenterBusiness模型中定义了附加的关联。   我说的是center.business认为商业这个术语   案件是武断的。但是,我需要实际参考   关系。因此,我已经设置了适当的密钥   CenterBusiness是信息。

我仍会接受任何更新和/或更好的方法来处理这种情况,但我认为这是当时最好的路线。

答案 1 :(得分:0)

以下示例可以帮助你 更多详情http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html

class User(Base):
     __tablename__ = 'user'
     id = Column(Integer, primary_key=True)
     name = Column(String(64))

     # association proxy of "user_keywords" collection
     # to "keyword" attribute
     keywords = association_proxy('user_keywords', 'keyword')

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

class UserKeyword(Base):
     __tablename__ = 'user_keyword'
     user_id = Column(Integer, ForeignKey('user.id'), primary_key=True)
     keyword_id = Column(Integer, ForeignKey('keyword.id'), primary_key=True)
     special_key = Column(String(50))

      # bidirectional attribute/collection of "user"/"user_keywords"
      user = relationship(User,
            backref=backref("user_keywords",
                            cascade="all, delete-orphan")
        )

    # reference to the "Keyword" object
    keyword = relationship("Keyword")

    def __init__(self, keyword=None, user=None, special_key=None):
        self.user = user
        self.keyword = keyword
        self.special_key = special_key

class Keyword(Base):
   __tablename__ = 'keyword'
   id = Column(Integer, primary_key=True)
   keyword = Column('keyword', String(64))

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

  def __repr__(self):
       return 'Keyword(%s)' % repr(self.keyword)