loopbackio / loopback-next

LoopBack makes it easy to build modern API applications that require complex integrations.
https://loopback.io
Other
4.95k stars 1.07k forks source link

Using hasMany relation and cascading ORM level in loopback4 #3526

Closed sunilsmith closed 3 years ago

sunilsmith commented 5 years ago

I have created 2 models Author and Books where Author has hasMany relation with Book and Book has belongsTo relation with author. While saving data using ORM models the cascading is not happening i.e

{
    "authorId": 1,
    "name": "qwery",
    "experience": 2,
    "books": [{
        "BookId": 12,
        "category": "string"
    }]
}

The above should create a Author record in Author table and create a Book record with the authorId in Book table, which is not happening whereas from belongsTo it can able to create an Author record with just authorId. can find the code in the following GIT

dhmlau commented 5 years ago

Transferred to loopback-next repo because this is about LB4. @sunilsmith, cross-posting my response to your stackoverflow post:


You need to create a AuthorBookController to connect the two. Please see example code in here: https://github.com/strongloop/loopback-next/blob/master/examples/todo-list/src/controllers/todo-list-todo.controller.ts

sestienne commented 5 years ago

Hi,

Why not in author controller just call in deleteById function : this.authorRepository.books(id).delete(); ?

The function deleteById of a repository is able to take Options, the reference api doesn't describe them. What are them ? Not possible to give a cascade option ? (method) DefaultCrudRepository<Category, number | undefined, CategoryRelations>.deleteById(id: number | undefined, options?: AnyObject | undefined): Promise<void> Thanks a lot!

bajtos commented 4 years ago

While saving data using ORM models the cascading is not happening

LoopBack does not support cascading operations (create, update, delete).

stale[bot] commented 3 years ago

This issue has been marked stale because it has not seen activity within six months. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository. This issue will be closed within 30 days of being stale.

stale[bot] commented 3 years ago

This issue has been closed due to continued inactivity. Thank you for your understanding. If you believe this to be in error, please contact one of the code owners, listed in the CODEOWNERS file at the top-level of this repository.