I want to delete the parent row if the associated rows in child tables have been removed.
class Child(Base):
__tablename__ = "children"
id = Column(Integer, primary_key=True)
parent_id = Column(Integer, ForeignKey("parents.id", ondelete='CASCADE'))
class Parent(Base):
__tablename__ = "parents"
id = Column(Integer, primary_key=True)
child = relationship(Child, backref="parent", passive_deletes=True)
If I remove the child
child_obj = session.query(Child).first()
session.delete(child_obj)
session.commit()
It does delete the child obj but parent remains as it is. I want to remove the parent as well using cascading.
A foreign key with cascade delete means that if a record in the parent table is deleted, then the corresponding records in the child table will automatically be deleted. This is called a cascade delete in SQL Server.
The correct way is to create the foreign key on mail to reference account . With ON DELETE CASCADE , you tell MySQL that it should delete a row (whose table has the foreign key) if its parent (referenced by the key) is deleted.
I rarely allow the deletion of parent records. My parent tables all have a bit field (column) named Active with a default value of -1 and a varchar field named ModifiedBy. If the user wishes to delete the record Active is valued at zero and the field ModifiedBy is updated with the user's name.
In the top left, tap Menu Remove account Remove account. Select your email address, then enter your password. If your child's device isn't connected to the Internet, use a parent access code to remove their account. On your parent device, open the Family Link app .
On your parent device, open the Family Link app . Select your child. Scroll down to the card for your child’s Android device. Tap Settings Reset device & delete data. Tip: To remove your child’s account from an Android device, you can click on your child’s name at g.co/YourFamily . You may need to sign in to your parental Google Account.
Parental supervision still exists for the account, but not on that specific device. You can add the removed account to a new device. Your child can still access emails, contacts, and photos associated with their account on other devices. Your child’s account isn’t “deleted” and may remain signed in on other devices.
You can read this thread: Linking Relationships with Backref
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
name = Column(String)
addresses = relationship("Address", backref="user")
class Address(Base):
__tablename__ = 'address'
id = Column(Integer, primary_key=True)
email = Column(String)
user_id = Column(Integer, ForeignKey('user.id'))
And you can define it in your child class:
parent = relationship(Parent, backref=backref("children", cascade="all,delete"))
You could do this for simple* cases by creating a listener that intercepts deletions of Child
instances and deletes the parent if there are no other children.
import sqlalchemy as sa
@sa.event.listens_for(sessionmaker, 'persistent_to_deleted')
def intercept_persistent_to_deleted(session, object_):
# We are only interested in instances of Child.
if not isinstance(object_, Child):
return
p = object_.parent
# Handle null parents.
if p is None:
return
cs = session.query(Child).filter(Child.parent == p).count()
if cs == 0:
session.delete(p)
* I would recommend thorough testing if your code is doing things like deleting children and then creating new children with the deleted children's parents in the same session. The listener works in this case:
c = session.query(Child).first()
p = c.parent
session.delete(c)
c1 = Child(parent=p)
session.add(c1)
session.commit()
but hasn't been tested on anything more complicated.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With