[sqlalchemy] Cascade Delete, session confusion
Hi all, I just started using SQLA, and I am confused by some cascade delete behaviour I am seeing. Please see the code and tests below which show that the session is seeing table rows in one area and not in the other: CODE: print Product1Mod3 __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) TESTING: 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() print \nQuery to check for changes with products and modules, shows that the modules and product are gone:\n print Product.query.all() print Module.query.all() print \nThe modules below belong to the deleted product, they should have disappeared, but do not: -- NOT SURE WHY THIS IS HAPPENING print Product1Mod1 print Product1Mod2 print Product1Mod3 -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Cascade Delete, session confusion
On Mar 9, 2014, at 3:18 PM, Dmitry Berman dmikha...@gmail.com wrote: print \nQuery to check for changes with products and modules, shows that the modules and product are gone:\n print Product.query.all() print Module.query.all() print \nThe modules below belong to the deleted product, they should have disappeared, but do not: -- NOT SURE WHY THIS IS HAPPENING print Product1Mod1 print Product1Mod2 print Product1Mod3 not sure what you're expecting there, are you expecting that the Product1Mod1 symbol would be modified within your interpreter to be None? Python can't do that under normal circumstances. A variable always points to the thing that it was assigned to, there's no (normal, non-hacky) mechanism by which variables change into None without explicitly being reassigned. Those product objects represent what used to be in those rows. They have a deleted flag you can see: from sqlalchemy import inspect inspect(deleted_product).deleted True -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
Re: [sqlalchemy] Cascade Delete, session confusion
This makes a lot of sense, I just didn't realize how it worked... *When I did the following:* print inspect(Product1Mod1).deleted print inspect(Product1Mod2).deleted print inspect(Product1Mod3).deleted *Instead of just:* print Product1Mod1 print Product1Mod2 print Product1Mod3 This returned: True True True And this makes total sense now. Thanks. On Sunday, March 9, 2014 7:22:49 PM UTC-4, Michael Bayer wrote: On Mar 9, 2014, at 3:18 PM, Dmitry Berman dmik...@gmail.com javascript: wrote: print \nQuery to check for changes with products and modules, shows that the modules and product are gone:\n print Product.query.all() print Module.query.all() print \nThe modules below belong to the deleted product, they should have disappeared, but do not: -- NOT SURE WHY THIS IS HAPPENING print Product1Mod1 print Product1Mod2 print Product1Mod3 not sure what you’re expecting there, are you expecting that the “Product1Mod1” symbol would be modified within your interpreter to be None? Python can’t do that under print inspect(Product1Mod1).deleted print inspect(Product1Mod2).deleted print inspect(Product1Mod3).deletednormal circumstances. A variable always points to the thing that it was assigned to, there’s no (normal, non-hacky) mechanism by which variables change into “None” without explicitly being reassigned. Those product objects represent what used to be in those rows. They have a “deleted” flag you can see: from sqlalchemy import inspect inspect(deleted_product).deleted True -- You received this message because you are subscribed to the Google Groups sqlalchemy group. To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at http://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.