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

Remove entity using cascade options #151

Closed
royibernthal opened this issue Dec 26, 2016 · 12 comments
Closed

Remove entity using cascade options #151

royibernthal opened this issue Dec 26, 2016 · 12 comments
Assignees
Milestone

Comments

@royibernthal
Copy link

royibernthal commented Dec 26, 2016

I have 2 entities - EntityA and EntityB, they have a OneToOne relation with cascade options:

EntityA.ts

@OneToOne(type => EntityB, entityB => entityB.entityA, {
        cascadeInsert: true,
        cascadeUpdate: true,
        cascadeRemove: true
    })
entityB: EntityB;

Cascade options work for insert and update so far, but how do I make it work for remove?

For instance, I have:

var entityA: EntityA = *finding in repo, inner join and select entityB*;

entityA.entityB exists.

I tried to then null it like that:

entityA.entityB = null;

When I persist entityA, it simply breaks the connection between the entities (makes sense).
I'd like it to remove entityB, similarly to:

entityBRepository.remove(entityB);

How do I do that?

@pleerock
Copy link
Member

There is one small bug I know with cascade remove in one-to-one relations. It should work fine with many-to-one/one-to-many relation, but does not in one-to-one relation. Maybe its because of that. I'll try to fix it this week. Would be great if you provide a minimum code that produces your error

@pleerock pleerock added the bug label Dec 26, 2016
@royibernthal
Copy link
Author

royibernthal commented Dec 26, 2016

I reproduced it with minimum code.
For some reason Github doesn't let me upload this ZIP, so I uploaded it to DropBox:
https://www.dropbox.com/s/3jrjzxed011ah95/typeorm_repr.zip?dl=1

Naturally, you'll need to change the db connection details in app.ts and recompile, and execute npm install to install the required dependencies.

Notice the rows in the db tables once you run node.

@royibernthal
Copy link
Author

Hey, any news about this?

@pleerock
Copy link
Member

fixed and released in typeorm@0.0.7-alpha.18

@royibernthal
Copy link
Author

Would it be possible to allow having remove cascade options on both sides of the relation (since both can be needed under different circumstances) and break circular removals when they happen?

@pleerock
Copy link
Member

cascade removes are real pain. if you set cascade remove from both sides it will be logically incorrect. If you just set post.category = null; and between them circular cascade removes, then logically category should be removed and since category.post relation exist and there is cascade remove between them post in this case also should be removed, but this wasn't expected by user when he did just post.category = null;

@royibernthal
Copy link
Author

royibernthal commented Jan 14, 2017

I understand the circular issue, but what I'm suggesting is that you track which relation caused the removal of an entity, and if that removal causes further removals that contain the removing entity, exit the recursion.
It might be a real pain to implement though depending on how your current implementation looks.

@pleerock
Copy link
Member

I don't think that preventing it from removing is correct behaviour either.

@royibernthal
Copy link
Author

For me it'd feel intuitive, but I guess that's very subjective. Wouldn't it be better than not allowing it altogether?

@royibernthal
Copy link
Author

Anyhow, it doesn't work for me also when there's no circular issue and it's defined only on one side of the relation.
Here's another reproduction:
https://www.dropbox.com/s/xculrkptrjyatb1/typeorm_repr_cascade_remove.zip?dl=1

@pleerock
Copy link
Member

pleerock commented Feb 3, 2017

this issue is not completely resolved, I'll return back to it in the near future

@pleerock
Copy link
Member

pleerock commented Nov 9, 2017

per our discussion with @AlexMesser we are removing cascade remove functionality from ORM scope. You must use onDelete relation option and setup a database-level cascades to resolve your issue.

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

2 participants