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ù)表示天機(jī)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ù)器、虛擬主機(jī)、免備案服務(wù)器”等云主機(jī)租用服務(wù)以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務(wù)可用性高、性價比高”等特點(diǎn)與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應(yīng)用場景需求。

分享名稱:Sqlalchemy中relationship的理解-創(chuàng)新互聯(lián)
文章來源:http://muchs.cn/article10/dhejdo.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、定制網(wǎng)站、網(wǎng)頁設(shè)計公司、App設(shè)計、搜索引擎優(yōu)化、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)