Delete Orphaned Parent
Some Ideas:
You could delete orphaned parents in
anafter_destroy
(find them using a
statement like the one on
http://groups.google.com/group/rubyonrails-talk/browse_thread/thread/a3f12d578f5a2619)You could set some instance variable in
before_destroy
containing the parent's ID, then do a lookup based on this id in anafter_destroy
callback and decide whether to delete the parent there based on counting the children
Delete nested rows from table with orphaned parent id
You can redefine the table:
CREATE TABLE tablename (
`id` INTEGER PRIMARY KEY,
`parentId` INTEGER,
`title` VARCHAR(6),
FOREIGN KEY (`parentId`) REFERENCES tablename(`id`) ON DELETE CASCADE
);
so that the column parentId
is a foreign key referencing the column id
which must be the PRIMARY KEY
of the table or must have a UNIQUE
index/constraint.
The ON DELETE CASCADE
action makes sure that when you delete a row all the children rows will also be deleted (also the children of the children rows and so on).
Also instead of 0
you must set the top level items to null
so there is no foreign key constraint violation.
You must turn on foreign key support because it is off by default and this can be done in SQLiteOpenHelper
's onConfigure()
method:
@Override
public void onConfigure(SQLiteDatabase db){
db.setForeignKeyConstraintsEnabled(true);
}
Now when you delete a row from the table all the rows of the levels below the level of the row that you deleted will also be deleted.
You may have to uninstall the app from the device so that the database is deleted and rerun to recreate the database and the table with the new definition.
See a demo.
Delete orphans when deleting parent manyToOne annotaion
If you want to keep using the association as unidirectional only, you can define the lazy-loaded inverse side in a field without exposing getters and setters for it:
@Entity
public class Patient {
@OneToMany(mappedBy = "patient", orphanRemoval = true)
private Collection<Appointment> appointments;
}
This way orphanRemoval
logic is applied from patients to their appointments and as a bonus you get the ability to navigate from patients to appointments in HQL queries.
Notice the mappedBy
attribute which tells that appointments are responsible for the association management, so you continue to associate appointments with patients by setting patients in the many-to-one relation defined in the Appointment
.
Orphan Removal on removing one child record, cascade= all-delete-orphan exception
You are deleting an element that is still referenced in the parent.
With orphanRemoval
removing a child element is as simple as:
parent.getChildren().remove(childToBeRemoved);
and then, if this is not done within a transaction:
em.merge(parent);
Sequelize orphan removal when deleting parent
You indicated onDelete: 'CASCADE'
and this option will work but for sequelize.sync
call only (to create a foreign key with this option). Usual destroy
calls don't take into account this option. So you should manually change an exising foreign key to set ON DELETE CASCADE
.
Nhibernate: Delete orphan children but don't delete children when parent is deleted
I don't believe you can do what you want via configuration.
The only option I can think of is turn off orphan delete, and manually delete the child when you want it deleted.
NHibernate Cascades: the different between all, all-delete-orphans and save-update
Here is what each cascade option means:
- none - do not do any cascades, let the users handles them by themselves.
- save-update - when the object is saved/updated, check the assoications and save/update any object that require it (including
save/update the assoications in many-to-many scenario).- delete - when the object is deleted, delete all the objects in the assoication.
- delete-orphan - when the object is deleted, delete all the objects in the assoication. In addition to that, when an object is removed
from the assoication and not assoicated with another object
(orphaned), also delete it.- all - when an object is save/update/delete, check the assoications and save/update/delete all the objects found.
- all-delete-orphan - when an object is save/update/delete, check the assoications and save/update/delete all the objects found. In
additional to that, when an object is removed from the assoication and
not assoicated with another object (orphaned), also delete it.
Related Topics
Generating an Instagram- or Youtube-Like Unguessable String Id in Ruby/Activerecord
How to Format This International Phone Number in Rails
How to Access Attributes Using Nokogiri
How to Customize Rails Activerecord Validation Error Message to Show Attribute Value
How to Use Gems Not in a Gemfile When Working with Bundler
Why Doesn't "Rails S" Work from the App Directory
Rails Carrierwave Base64 Image Upload
Rails - Local Variables Versus Instance Variables
How to Generate Links with Trailing Slash in Rails 3
Complicated Graphviz Tree Structure
How to Convert a Ruby Object to JSON
What Is '-Mix' in a Ruby Regular Expression
Ruby (And Rails) Nested Module Syntax
What Is the Best Practice When It Comes to Testing "Infinite Loops"
Why Do I Get an Error Installing the JSON Gem in Ubuntu