簡體   English   中英

SQLAlchemy 1.4 與關聯表的多對多關系重疊關系的警告

[英]SQLAlchemy 1.4 warnings on overlapping relationships with a many-to-many relationship with association table

我在 SQLAlchemy 中有一個 model ,它使用關聯表定義了多對多關系(這里使用automap映射,因為我使用的是現有數據庫):

from sqlalchemy import (Column, Table, MetaData, Integer, Text, LargeBinary,
                        ForeignKey, Float, Boolean, Index)
from sqlalchemy.ext.automap import automap_base, AutomapBase
from sqlalchemy.orm import Session, deferred, relationship

Base: AutomapBase = automap_base()


class VariantAssociation(Base):

    __tablename__ = "sample_variant_association"

    vid = Column(Integer, ForeignKey("variants.variant_id"),
                 primary_key=True)
    sid = Column(Integer, ForeignKey("samples.sample_id"),
                 primary_key=True)

    vdepth = Column(Integer)
    valt_depth = Column(Integer)
    gt = Column(Text)
    gt_type = Column(Integer)
    fraction = Column(Float)

    variant = relationship("Variant", back_populates="samples")
    sample = relationship("Sample", back_populates="variants")

    __table_args__ = (Index('ix_sample_variant_association_valt_depth',
                            "valt_depth"),
                      Index('ix_sample_variant_association_vdepth',
                            "vdepth"),
                      Index('ix_sample_variant_association_vid', 'vid'),
                      Index('ix_sample_variant_association_sid', 'sid'),
                      Index('ix_sample_variant_association_fraction',
                            'fraction')
                      )


class Variant(Base):

    __tablename__ = "variants"

    variant_id = Column(Integer, primary_key=True)
    info = deferred(Column(LargeBinary))

    samples = relationship("VariantAssociation",
                           back_populates="variant")

class Sample(Base):

    __tablename__ = "samples"

    sample_id = Column(Integer, primary_key=True, index=True)
    name = Column(Text, index=True)
    variants = relationship("VariantAssociation",
                            back_populates="sample")


class SampleGenotypeCount(Base):
    __tablename__ = 'sample_genotype_counts'

    sample_id = Column(Integer, primary_key=True)
    num_hom_ref = Column(Integer)
    num_het = Column(Integer)
    num_hom_alt = Column(Integer)
    num_unknown = Column(Integer)


class DataMigration(Base):

    __tablename__ = "datamigration"
    done = Column(Boolean, primary_key=True)

在查詢時,這最終會生成以下警告:

詢問:

query = session.query(Variant).join(
            Variant.samples).join(Sample)

警告:

/usr/local/lib/python3.9/site-packages/sqlalchemy/orm/relationships.py:3441: SAWarning: 
relationship 'Variant.variantassociation_collection' will copy column variants.variant_id to 
column sample_variant_association.vid, which conflicts with relationship(s): 'Variant.samples' 
(copies variants.variant_id to sample_variant_association.vid). If this is not the intention, 
consider if these relationships should be linked with back_populates, or if viewonly=True 
should be applied to one or more if they are read-only. For the less common case that foreign 
key constraints are partially overlapping, the orm.foreign() annotation can be used to isolate 
the columns that should be written towards.   The 'overlaps' parameter may be used to remove 
this warning. (Background on this error at: http://sqlalche.me/e/14/qzyx)

我一直在查看 SO 和 SQLAlchemy 文檔,但我找不到可能導致此問題的原因,因為(在我看來) back_populates參數位於正確的位置。

model 中的錯誤在哪里? SQLAlchemy 1.3.23 沒有生成一個,FTR。

為了設置自己的關系名稱,您需要防止 Automap 自己生成關系。 您可以通過將“generate_relationship”設置為返回 None 的 function 來實現此目的。

def generate_relationships(base, direction, return_fn, attrname, local_cls, referred_cls, **kw):
    return None

Base.prepare(generate_relationship=generate_relationships)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM