首页 > 其他分享 >SAWarning: relationship X will copy column Q to column P, which conflicts with relationship(s): Y

SAWarning: relationship X will copy column Q to column P, which conflicts with relationship(s): Y

时间:2024-07-22 16:18:20浏览次数:10  
标签:__ SAWarning parent column Column relationship Child id

在sqlalchemy之中,当一个字段对应多个relationship的时候。因为ORM要处理flush操作,而两个relationship可能都涉及到flush,以至于ORM无法同时兼顾。这时,sqlalchemy就会发出一个SAWarning。

为了避免该类事件,可以通过以下配置来实现。

假设,存在Parent和Child两个表,其中Child.parent_id的外键对应Parent.id这个字段

class Parent(Base):
    __tablename__ = "parent"
    id = Column(Integer, primary_key=True)


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

 

1. 配置relationship.back_populates

如果,我们需要在Parent中处理children,并且在Child中来处理parent。那么可以使用如下方法。

class Parent(Base):
    __tablename__ = "parent"
    id = Column(Integer, primary_key=True)
    children = relationship("Child")


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

但是,这样处理会报如下warning:

SAWarning: relationship 'Child.parent' will copy column parent.id to column child.parent_id,
which conflicts with relationship(s): 'Parent.children' (copies parent.id to child.parent_id).

大概意思是Child.parent和Parent.children这两个relationship发生了冲突。

这时,我们就可以通过配置back_populates,来消除Warning,具体如下:

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(ForeignKey("parent.id"))
    parent = relationship("Parent", back_populates="children")

2. 配置relationship.overlaps

如果我们就是需要两个relationship对应同一个字段呢?比如,下面这种情况,Child.flag==1需要一个relationship,Child.flag==0也需要一个relationship。这时可以通过配置overlaps来实现,具体如下:

class Parent(Base):
    __tablename__ = "parent"
    id = Column(Integer, primary_key=True)
    c1 = relationship(
        "Child",
        primaryjoin="and_(Parent.id == Child.parent_id, Child.flag == 0)",
        backref="parent",
        overlaps="c2, parent",
    )
    c2 = relationship(
        "Child",
        primaryjoin="and_(Parent.id == Child.parent_id, Child.flag == 1)",
        overlaps="c1, parent",
    )


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

    flag = Column(Integer)

3. 配置relationship.viewonly

还有一种情况,如果我们确实需要多次引用parent_id,但是只是在查询中使用,为了查询方便,根本没有想通过relationship来flush的目的。这时可以配置viewonly=True,具体如下:

class Parent(Base):
    __tablename__ = "parent"
    id = Column(Integer, primary_key=True)
    children = relationship("Child", back_populates="parent")
    children_num=relationship("Child", back_populates="parent",viewonly=True)


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

 

标签:__,SAWarning,parent,column,Column,relationship,Child,id
From: https://www.cnblogs.com/DidierFeng/p/18316271

相关文章

  • 用navicat导入数据时,报错: [Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:0
    原因这是因为当前的MySQL(作者是5.7.23)不支持datetime为0的情况。解决方法1:修改sql_modesql_mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。查看全局sql_mode:select@@global.sql_mode;可以看到,O_ZERO_DATE、NO_ZERO_I......
  • Python - Pandas - loc vs iloc (DataFrame.loc[:,['column_name':]])
    原文链接:https://blog.csdn.net/weixin_48964486/article/details/123150832————————————————————————————————————————————————关于python数据分析常用库pandas中的DataFrame的loc和iloc取数据基本方法总结归纳及示例如下:1.......
  • sqlalchemy中relationship使用
    sqlalchemy是python做orm管理的非常重要的工具,sqlalchemy2.0版本relationship与上个版本有所不同,具体如下:fromsqlalchemyimportcreate_enginefromsqlalchemy.ormimportsessionmakerfromsqlalchemy.ormimportdeclarative_baseSQLALCHEMY_DATABASE_URL='你的数据库......
  • codeforces 1980 E. Permutation of Rows and Columns
    题目链接https://codeforces.com/problemset/problem/1980/E题意共输入\(T\)组测试用例,每组测试用例第一行输入两个整数\(n,m\),分别代表输入的数据的行数和列数,其中\(n*m\leq2*10^5\)。接下来输入两个\(n\)行\(m\)列的矩阵\(a,b\),对于每个矩阵中的元素\(x_{i,j}\)都是......
  • WPF generate rows and columns via C# dynamically
    //xaml<Windowx:Class="WpfApp214.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.mi......
  • 1055 - Expression #9 of SELECT list is not in GROUP BY clause and contains nonag
    MySQL8的默认sql_mode包含了only_full_group_by,如果想要sql不按照这模式做检查,可以设置当前session的sql_mode值不包含oly_full_group_by;全局修改则使用以下sql--全局配置session级配置则去掉GlobalSETGLOBALsql_mode='ANSI_QUOTES,STRICT_ALL_TABLES,STRICT_TRANS_TAB......
  • feature column
    embedding_column和featurecolumn是什么区别?embedding_column是featurecolumn的一种类型embeddingcolumn体现在graph上和代码上是这样的这是一个featurecolumn的例子,能够能好的理解featurecolumn和embeddingcolumn的关系Featurecolumn的计算大概分两步第一步......
  • ArcTs布局入门01——线性布局(Row/Column)
    如果你对鸿蒙开发感兴趣,加入“Harmony自习室”吧~......
  • WPF grid column resize via GridSpitter, when you can drag to enlarge or shrink t
    <Windowx:Class="WpfApp137.MainWindow"xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"xmlns:d="http://schemas.microsoft......
  • dbt dbt-audit-helper 包compare_relation_columns 处理简单说明
    dbtdbt-audit-helper包在进行compare_relation_columns处理的时候进行数据表列字段创建顺序的判断参考使用我按照test处理的,同时进行的测试异常进行存储使用{{audit_helper.compare_relation_columns(a_relation=source("dalongdemo","mytest_appv2")......