-
Notifications
You must be signed in to change notification settings - Fork 123
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
Cascade Delete #423
Comments
Right now the delete method mimics SQL's If you take a look at this block of code it becomes clear what you need to do. Lines 758 to 779 in 3eccce7
I haven't tested this but in theory you could extend the Model class and overwrite the import { Model } from "redux-orm";
import ForeignKey from "redux-orm/fields/ForeignKey";
import ManyToMany from "redux-orm/fields/ManyToMany";
import OneToOne from "redux-orm/fields/OneToOne";
class CascadingDeleteModel extends Model {
delete() {
const { virtualFields } = this.getClass();
// eslint-disable-next-line guard-for-in, no-restricted-syntax
for (const key in virtualFields) {
const field = virtualFields[key];
if (field instanceof ManyToMany) {
this[key].delete();
} else if (field instanceof ForeignKey) {
this[key].delete();
} else if (field instanceof OneToOne) {
const other = this[key];
if (other !== null) {
other.delete();
}
}
}
super.delete();
}
} It's possible that the many-to-many deletion does not work because it then can't delete the through model instances anymore (as it doesn't find any). |
It seems incredibly verbose and low level, and requires a lot of deciphering the source. If the object of the library is to simulate a SQL database on the frontend as a Redux store, then it should have some of these standard DELETE options built-in in my opinion. |
You're right, this could be part of Redux-ORM. However I don't know what a good API would look like. Adding to the API surface area is always a dangerous move and it should have some foresight. Open to suggestions. |
I would probably accept another argument in the relationship where you define your relationship fields just as you would in any other ORM, backend or frontend. Personally, I come from a Django background in which the https://stackoverflow.com/questions/38388423/what-does-on-delete-do-on-django-models As it now stands, the API already supports the first two arguments. Obviously easier said than done, because I certainly can't decipher the source and submit a PR, but you'd have to add that last argument for delete events. Not a related issue, but you should also decouple models from ORM instances entirely such that they can be handpicked and reused in different instances as mentioned in #417 and #422. And support a bulk fetch where you can set the model's entire QuerySet on a GET list request instead of having to manually clear if need be, followed by a |
Any updates on this? |
Is there a way to setup Redux-ORM such that related models cascade delete by default?
The text was updated successfully, but these errors were encountered: