Skip to content

Many-to-Many relationships cannot be cleaned up inside beforeDelete() #14585

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

Open
4 tasks done
joemccall86 opened this issue Feb 21, 2018 · 1 comment
Open
4 tasks done

Comments

@joemccall86
Copy link
Contributor

Task List

  • Steps to reproduce provided
  • Stacktrace (if present) provided
  • Example that reproduces the problem uploaded to Github
  • Full description of the issue provided (see below)

Steps to Reproduce

  1. Create domain classes with a many-to-many relationship
  2. Attempt to clean up the generated join table inside of the beforeDelete() method (utilizing withNewSession as outlined here: http://gorm.grails.org/6.0.x/hibernate/manual/#_the_beforedelete_event)
  3. Attempt to delete one side of the relationship

Expected Behaviour

The beforeDelete() method is able to remove the object being deleted from other associations in the database.

Actual Behaviour

An exception is thrown when the session is flushed:

Hibernate operation: could not execute statement; SQL [n/a]; Referential integrity constraint violation: "FKGHKKY8WMH379RPMFH92T807RY: PUBLIC.TEAM_MEMBERS FOREIGN KEY(PERSON_ID) REFERENCES PUBLIC.PERSON(ID) (1)"; SQL statement:
delete from person where id=? and version=? [23503-194]; nested exception is org.h2.jdbc.JdbcSQLException: Referential integrity constraint violation: "FKGHKKY8WMH379RPMFH92T807RY: PUBLIC.TEAM_MEMBERS FOREIGN KEY(PERSON_ID) REFERENCES PUBLIC.PERSON(ID) (1)"; SQL statement:
delete from person where id=? and version=? [23503-194]

Notes

Following the advice from GORM Gotchas (Part 2) I understand that many-to-many relationships do not cascade deletions, so I need to manage them on my own. The domain's beforeDelete() method seemed like the perfect place to document this and other side-effects of deleting an instance.

If this is not possible I think there should be a section in the documentation that explains a recommended way of cleaning up associated join tables for many-to-many relationships. I think this situation is common enough to warrant it. I don't mind writing that section if it's confirmed that the sample project exhibits correct behavior.

Environment Information

  • Operating System: Ubuntu MATE 17.10
  • GORM Version: 6.1.8.RELEASE, 6.0.13.RELEASE
  • Grails Version (if using Grails): 3.2.12
  • JDK Version: openjdk version "1.8.0_162"

Example Application

@RobertStroud
Copy link
Contributor

I have also encountered this issue, also with Grails 3.2.12 but using GORM 6.1.6.RELEASE.

I tried using beforeDelete() to resolve the problem but concluded...

"For some reason, this cannot be done in beforeDelete, possibly because GORM checks whether the delete is valid before calling beforeDelete"

@jdaugherty jdaugherty transferred this issue from apache/grails-data-mapping Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants