Skip to content

relation expects foreignKey on the Object #1719

@virkt25

Description

@virkt25

Description / Steps to reproduce / Feature proposal

When I was working on user order history component for https://github.com/strongloop/loopback4-example-shopping I ran into an issue ...

/users/{userId}/orders expects the orders object to have a userId field (Since it's a required property of the Order model and is enforced by the controller coercion / validation). The issue is that passing in the userId on the Order object passes controller validation but causes the relation validation to throw an error saying the foreign key can't be changed -- despite the fact that the foreign key is exactly the same as the one in the URL and the one given to the repository.

Current Behavior

Throws error if foreign key on object saying you can't change it even if it's the same one.

Expected Behavior

Be smart enough to not throw an error if the key given is the same as the one that's expected.


My current solution is to delete the foreign key in the controller. The other alternatives I can think of is to mark the key as an optional property (do we want this?) ... or to mark the requestBody type as Partial<Order> but this will mean losing type validation of expected properties on the Order type.

See Reporting Issues for more tips on writing good issues

Metadata

Metadata

Assignees

No one assigned

    Labels

    RelationsModel relations (has many, etc.)bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions