Skip to content

Throw a helpful error when calling attach, detach, or sync on unloaded entities #93

@elpete

Description

@elpete

The most common place this happens is using relationship setters and passing an array of entities to sync to an unloaded entity, like so:

post.create( {
    "title": "Some title",
    "body": "Some post body",
    "tags": [ "foo", "bar" ] // this will fail
} );

It needs to be

post.create( {
    "title": "Some title",
    "body": "Some post body"
} );
post.tags().attach( [ "foo", "bar" ] );

We could try to do some magic by adding a postInsert hook and attaching the tags there, but it seems to approach the problem of Hibernate where it did too much for you. I think I'd rather just throw a helpful error message that points the user to saving then calling attach separately. I also like how it accurately shows that this will take two queries because that is what would happen if we did it automagically.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions