python批量删除字段,python – SQLAlchemy已加入继承快速批量删除Child对象

在尝试了一两个小时之后,我找到了一个没有太多代码的解决方案.然后我会重现它.

我检查了delete()的文件.有两句话:

This method does not work for joined inheritance mappings, since the multiple table deletes are not supported by SQL as well as that the join condition of an inheritance mapper is not automatically rendered

However the above SQL will not delete from the Engineer table, unless an ON DELETE CASCADE rule is established in the database to handle it.

Short story, do not use this method for joined inheritance mappings unless you have taken the additional steps to make this feasible.

因此,定义外键约束是必要的.像这样:

class Location(Base):

__tablename__ = 'location'

id = Column(INTEGER, primary_key=True)

name = Column(VARCHAR(30))

type = Column(VARCHAR(30))

__mapper_args__ = {

'polymorphic_identity': 'location',

'polymorphic_on' : type,

}

class Field(Location):

__tablename__ = 'field'

id = Column(INTEGER, ForeignKey('location.id', ondelete='cascade'), primary_key=True)

size = Column(DECIMAL(20, 2))

__mapper_args__ = {

'polymorphic_identity': 'field',

}

2.现在,如果我们删除Location,Field中的行也将被删除.

session.query(Location).filter(Location.id == 1).delete()

但是,海报想要删除字段而不是位置.

session.query(Field).filter(Field.size < 5).delete()

这只删除了Field中没有行的行.因为Field是外表,所以它不能级联主表.

那么,现在我们应该做的是根据Field.size

session.query(Location).filter(Field.size < 5).delete()

session.query(Location).outerjoin(Field, Location.id == Field.id).filter(Field.size < 5).delete()

这两个都抛出异常.

经过多次尝试,我找到的解决方案是这样的:

statment = delete(Field, prefixes=[Location.__tablename__]).where(Field.size == 1)

session.execute(statment)

生成的sql是DELETE位置FROM位置JOIN字段ON location.id = field.id WHERE field.size

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值