Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feature] Enable Cascade Behavior for generic_relationship #723

Open
visuman opened this issue Jan 3, 2024 · 0 comments
Open

[Feature] Enable Cascade Behavior for generic_relationship #723

visuman opened this issue Jan 3, 2024 · 0 comments

Comments

@visuman
Copy link

visuman commented Jan 3, 2024

Description
I would like to propose the addition of cascade behavior to the generic_relationship feature in SQLAlchemy-Utils. Currently, the absence of cascade options on generic relationships limits their ability to automatically propagate changes to linked tables.

Example
Consider the following example:

class User(Base):
    __tablename__ = 'users'
    id = sa.Column(sa.Integer, primary_key=True)


class Customer(Base):
    __tablename__ = 'customer'
  id = sa.Column(sa.Integer, primary_key=True)

class Event(Base):
    __tablename__ = 'event'
    id = sa.Column(sa.Integer, primary_key=True)
    # This is used to discriminate between the linked tables.
    object_type = sa.Column(sa.Unicode(255))
    # This is used to point to the primary key of the linked row.
    object_id = sa.Column(sa.Integer)
    object = generic_relationship(object_type, object_id)

# Add user
user = User()
session.add(user)
session.commit()

# Add event
event = Event()
event.object = user
session.add(event)
session.commit()

# Delete user
user = session.query(User).first() # only 1 user
sessin.delete(user)
session.commit()


# Get event and print object, object_id
event = session.query(Event).first() # This should not have returned any result if cascade-delete was supported in generic_relationships

print(event.object)
# None

print(event.object_id)
# 1

In this example, when an instance of User is deleted, I expect the associated records in the Event table to be automatically deleted as well, assuming cascade behavior is enabled for the generic_relationship.

Additional Notes:

Enabling cascade behavior on generic_relationship would enhance the usability and consistency of generic relationships in SQLAlchemy-Utils, providing a more intuitive and comprehensive solution for managing relationships between tables.

Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant