Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete parent if the child is deleted

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.

like image 220
mad_ Avatar asked Aug 27 '18 18:08

mad_


People also ask

How do you delete parent and child records in SQL?

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.

How do you delete parent and child records in hibernate?

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.

Is it possible to delete parent records?

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.

How do I remove my child's email account from family link?

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 .

How do I delete data from my child's Android device?

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.

What happens to my child's account if I remove parental supervision?

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.


2 Answers

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"))
like image 70
Benjamin RD Avatar answered Oct 16 '22 11:10

Benjamin RD


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.

like image 1
snakecharmerb Avatar answered Oct 16 '22 09:10

snakecharmerb