python中Flask数据库sqlalchemyORM层面外键和一对一关系实现插入数据时报错relationship ‘UserExtend.user‘ will copy column user.

python中Flask数据库sqlalchemyORM层面外键和一对一关系实现插入数据时报错relationship ‘UserExtend.user’ will copy column user.id to column user_extend.uid, which conflicts with relationship(s): ‘User.extend’ (copies user.id to user_extend.uid). 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

ORM关系之一对一:
在sqlalchemy中,如果想要将两个模型映射成一对一的关系,那么应该在父模型中,指定引用的时候,要传递一个uselist=False这个参数进去。就是告诉父模型,以后引用这个从模型的时候,不再是一个列表了,而是一个对象了
使用ORM一对一有好处,但今天说的不是好处,是在创建一对一关系表时出现的错误
小伙伴们实现ORM一对一插入数据时是不是出现这个错误
在这里插入图片描述

报错原因

这是其中一种ORM一对一创建的方式,为什么会报错?
原因是:两个或多个关系将在刷新时将数据写入同一列,但ORM在这两个关系之间没有任何类型的反向填充配置。解决方法通常是安装正确的 sqlalchemy.orm.back_populates配置。
上面的映射将生成警告:

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:
在这里插入图片描述这里需要做指定主表,这样关系就不会冲突了。
第二种方式就不用指定:
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值