gpt4 book ai didi

python - SQLAlchemy 级联删除(可能是 session 困惑)

转载 作者:行者123 更新时间:2023-12-01 05:21:16 24 4
gpt4 key购买 nike

当父级被删除时,我试图将删除级联到子级中。根据这个问题SQLAlchemy: cascade delete我将 cascade="all, delete-orphan" 选项添加到子项的 backref 中。这在一定程度上似乎确实有效。子级实际上已从数据库中删除(通过 SQLlite 数据库浏览器确认),但子级仍然可见“python 端”:

代码:

class Product(Base):
__tablename__ = 'products'
id = Column(Integer, primary_key=True)
product_name = Column(String(250), unique=True)
vendor_id = Column(Integer, ForeignKey('vendors.id'), nullable=False)

vendor = relationship('Vendor', backref = backref('products', order_by=id, cascade="all, delete-orphan"))

def __init__(self, product_name, vendor_id):
self.product_name = product_name
self.vendor_id = vendor_id

def __repr__(self):
return '<Product: %r Product ID: %r Vendor ID: %r>' % (self.product_name, self.id, self.vendor_id)


class Module(Base):
__tablename__ = 'modules'
id = Column(Integer, primary_key=True)
module_name = Column(String(250), unique=True)
product_id = Column(Integer, ForeignKey('products.id'), nullable=False)

product = relationship('Product', backref = backref('modules', order_by=id, cascade="all, delete-orphan"))

def __init__(self, module_name, product_id):
self.module_name = module_name
self.product_id = product_id


def __repr__(self):
return '<Module: %r Module ID: %r Product ID: %r>' % (self.module_name, self.id ,self.product_id)

测试:

msg('Module Tests')
Product2Mod1 = Module('Product2Mod1',1)
Product2Mod2 = Module('Product2Mod2',1)
Product1Mod1 = Module('Product1Mod1',2)
Product1Mod2 = Module('Product1Mod2',2)
Product1Mod3 = Module('Product1Mod3',2)
db_session.add(Product2Mod1)
db_session.add(Product2Mod2)
db_session.add(Product1Mod1)
db_session.add(Product1Mod2)
db_session.add(Product1Mod3)
db_session.commit()
msg("Product2Mod1 Product:")
print Product2Mod1.product

msg('delete tests')

print "Query to show all products: \n"
print Product.query.all()

print "\nQuery to show all modules: \n"
print Module.query.all()

print "\ndeleting product 1: db_session.delete(Product1) --> db_session.commit()"
db_session.delete(Product1)
db_session.commit()
db_session.flush()

print "\nQuery to check for changes with products and modules (THIS IS CORRECT):\n"
print Product.query.all()
print Module.query.all()

print "\nThe modules below belong to the deleted product, they should have disappeared (But they did not, THIS IS INCORRECT):"
print Product1Mod1
print Product1Mod2
print Product1Mod3

最佳答案

实际上,SQLAlchemy 在这里的行为是正确的。要理解它,您必须远离 SQLAlchemy。您告诉图书馆删除其后端的某些内容。但是,您仍然可以引用您自己创建并刚刚提供的实例。以这个人工示例为例:

some_obj = MyClass()
backend_storage.store(some_obj)
backend_storage.delete(some_obj)

您希望 some_obj 现在在您的本地空间中发生什么?这个库应该删除你的变量吗?

SQLAlchemy 的行为方式如下:它知道这些对象已经消失,因此询问它们将不再返回它们。但是,您仍然在本地拥有它们并且它们已经存在于那里,因此只要您引用它们,它们就会保留在内存中。

另外一件事:这些对象是您自己创建的还是由库返回的,这并不重要。它们现在是“你的”,现在外部代码应该会弄乱它们。例如:

some_obj = backend_storage.load_one(MyClass)  # load the first object of MyClass
backend_storage.delete(some_obj)

这与上面相同:some_obj你的。如果您愿意,您甚至可以现在重新添加它,后端不会关心。

关于python - SQLAlchemy 级联删除(可能是 session 困惑),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22283916/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com