Allow deleting a `HasOne` (1-1) relation
## Motivation
The use case is to allow updating an entity with a `HasOne` relation, to delete the linked entity.
Currently when using `ActiveModelEx` you can use `HasOneModel::set` to set a new value for the field, but there is no way to just delete the value.
For example we might create a `User` with a `Subscription`, but we can't then easily delete the `Subscription` via the `User`, when it expires.
## Proposed Solutions
Add a `Delete` variant to `HasOneModel` that deletes the linked entity, and something like `HasOneModel::set_option(value: Option<E>)` that acts like `set` when the value is `Some`, and deletes the entity when value is `None`.
I imagine there'd also be some additions to the builder to allow `delete_{field_name}` and `set_option_{field_name}`, e.g. `delete_profile` in the example in the repo below.
## Additional Information
I asked about this on Discord and Huli recommended adding an issue.
I've created a [minimal example on github](https://github.com/trepidacious/delete-one-to-one). Note this also shows a bug that happens when using `HasOneModel::set` to replace an existing linked entity, this produces a unique constraint error. The suggested new `Delete` variant is shown as commented code on the entities from the repo.
0 条评论