Skip to content

Allow deleting a HasOne (1-1) relation #3060

@trepidacious

Description

@trepidacious

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. 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.

Metadata

Metadata

Assignees

Labels

No labels
No labels
No fields configured for Feature.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions