最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Sqlalchemy中relationship的理解-創(chuàng)新互聯(lián)

“指向”

成都創(chuàng)新互聯(lián)專注于賈汪企業(yè)網(wǎng)站建設(shè),成都響應(yīng)式網(wǎng)站建設(shè),商城網(wǎng)站定制開發(fā)。賈汪網(wǎng)站建設(shè)公司,為賈汪等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站策劃,專業(yè)設(shè)計,全程項目跟蹤,成都創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)

即定義一張表中的數(shù)據(jù)條目指向另一張表中的條目,建立這種有向的“指向”可以讓表以字段的方式查詢到被指向的條目(們),所以,如果要雙向查詢,就需要雙向指向。

One To Many

在“多”方表中添加“一”方的id作為ForeignKey約束,為查詢方便雙方均需要定義relationship()字段;

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
children = relationship("Child", back_populates="parent")
class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))
parent = relationship("Parent", back_populates="children")

One To One

與One To Many區(qū)別不大,只要“一”方的relationship方法中添加一個"uselist=False"參數(shù)即可,uselist是一個標(biāo)量屬性(a scalar attribute),其含義是“一”方對應(yīng)另一張表的條目不使用列表。

class Parent(Base):
__tablename__ = 'parent'
id = Column(Integer, primary_key=True)
child = relationship("Child", uselist=False, back_populates="parent")

class Child(Base):
__tablename__ = 'child'
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey('parent.id'))  # 多方才有這個字段
parent = relationship("parent", back_populates="child") 
# 通常也會把backref用函數(shù)表示天機uselist=False參數(shù),用以顯示指出。 
# parent = rlationship("parent", backref=backref("child", uselist=False))

Many To Many

借助中間表完成,利用relationship支持的secondary參數(shù),

association_table = Table('association', Base.metadata,
    Column('left_id', Integer, ForeignKey('left.id')),
    Column('right_id', Integer, ForeignKey('right.id'))
    )
    
class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship("Child",
                            secondary=association_table,
                            back_populates="parents")
class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)
    parents = relationship("Parent",
                            secondary=association_table,
                            back_populates="children")

注意,secondary可以接受'a callable that returns the ultimate argument,which is evaluated only when mappers are first used."即接受可執(zhí)行參數(shù),可以讓association_table 在稍晚的時候定義,甚至可以在所有模塊都初始化完成后,直到它可調(diào)用為止。

class parent(Base):
    __tablename__ = "left"
    id = Column(Integer, primary_key=True)
    children = relationship("Child", 
                            secondary=lambda: assciation_table,
                            backref="parents")

以上relationship參數(shù)的表明均可以用類名字符串代替。

刪除Mang To Many記錄,不必手動刪除secondary的中間表數(shù)據(jù),數(shù)據(jù)庫會根據(jù)“cascade rule”級聯(lián)規(guī)則自動刪除。

如果中間表對象需要被調(diào)用

class Association(Base):
    __tablename__ = 'association'
    left_id = Column(Integer, ForeignKey('left.id'), primary_key=True)
    right_id = Column(Integer, ForeignKey('right.id'), primary_key=True)
    extra_data = Column(String(50))
    child = relationship("Child", back_populates="parents")
    parent = relationship("Parent", back_populates="children")
class Parent(Base):
    __tablename__ = 'left'
    id = Column(Integer, primary_key=True)
    children = relationship("Association", back_populates="parent")
class Child(Base):
    __tablename__ = 'right'
    id = Column(Integer, primary_key=True)
    parents = relationship("Association", back_populates="child")
    
# create parent, append a child via association
p = Parent()
a = Association(extra_data="some data")
a.child = Child()
p.children.append(a)
# iterate through child objects via association, including association
# attributes
for assoc in p.children:
    print(assoc.extra_data)
    print(assoc.child)

# 尋找答案的路途上要保持耐心和專心!

需要注意:back_populates參數(shù)賦值參數(shù)一定不能是relationship第一個參數(shù)的字段,那樣相當(dāng)于被對應(yīng)關(guān)系表中有了重復(fù)字段。

另外有需要云服務(wù)器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務(wù)器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務(wù)器、裸金屬服務(wù)器、高防服務(wù)器、香港服務(wù)器、美國服務(wù)器、虛擬主機、免備案服務(wù)器”等云主機租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。


網(wǎng)站名稱:Sqlalchemy中relationship的理解-創(chuàng)新互聯(lián)
新聞來源:http://fisionsoft.com.cn/article/dhejdo.html