Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many to many relation (hasAndBelongsToMany) #2043

Open
jiwantmg opened this issue Nov 16, 2018 · 43 comments
Open

Many to many relation (hasAndBelongsToMany) #2043

jiwantmg opened this issue Nov 16, 2018 · 43 comments
Labels
feature Relations Model relations (has many, etc.)

Comments

@jiwantmg
Copy link

Description / Steps to reproduce / Feature proposal

I want to implement many to many relation in loopback4 but I couldn't see the article or any documentation regarding many to many relation

@koalabi
Copy link

koalabi commented Nov 19, 2018

Currently it is apparently not supported (only 1-to-many [hasMany] and the reciprocal relation [belongsTo] (see https://loopback.io/doc/en/lb4/Relations.html) but I have a similar need and I'd like to know (@raymondfeng) either for when it is planned on the roadmap and/or if it is possible to contribute to speed things up (and, if so, how to contribute).

@nm
Copy link

nm commented Nov 19, 2018

Description / Steps to reproduce / Feature proposal

I want to implement many to many relation in loopback4 but I couldn't see the article or any documentation regarding many to many relation

+1

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

Thanks for the feedback! It seems like there are more users who are looking for the other model relation types than what we have so far. As you might've known already, support of hasOne #1422 is in progress. Let me discuss with @raymondfeng and team and see if there's some document to get others started on adding more relation types.

@dhmlau dhmlau self-assigned this Nov 21, 2018
@dhmlau dhmlau mentioned this issue Nov 21, 2018
20 tasks
@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

cc @RaphaelDrai @elv1s

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

Hi all, thanks for your interest in contributing to more relation types! Would like to see who has a particular interest or need for a certain relation type. Not sure what's the best way to do a poll here, I'm going to add one comment for each relation type below, and if you would like to contribute or need it for your project, please upvote. Thanks!

Here are the outstanding relation types:
HasManyThrough
HasAndBelongsToMany
Polymorphic
EmbedsOne
EmbedsMany
ReferencesMany

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

  1. HasManyThrough

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

  1. HasAndBelongsToMany

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

  1. Polymorphic

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

  1. EmbedsOne

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

  1. EmbedsMany

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

  1. ReferencesMany

@dhmlau
Copy link
Member

dhmlau commented Nov 21, 2018

cc @strongloop/loopback-next @strongloop/loopback-maintainers

@koalabi
Copy link

koalabi commented Nov 21, 2018 via email

@SpeedoPasanen
Copy link

HasManyThrough, HasAndBelongsToMany.
Flick-the-switch type of Auto Update would be very nice. One that adds foreign keys automatically (which LB3 never could do), with cascade delete/restrict.

@clayrisser
Copy link
Contributor

How can I contribute to the manyToMany ??? I really need it.

@dhmlau
Copy link
Member

dhmlau commented Nov 22, 2018

@b-admike @bajtos , what do you think is the best way to let others join forces? some content around how to get started or how the code are being organized? Thanks.

@bajtos
Copy link
Member

bajtos commented Nov 23, 2018

I am proposing to keep this issue focused on "many to many" relation, and discuss other relation types in [EPIC] More Relations (post-GA) #1450.

From what I remember from LB3, "many to many" relation is called HasAndBelongsToMany and leverages HasManyThrough under the hood.

So I think the first step is to figure out how to implement a "hasManyThrough" relation.

In my experience so far, adding a new relation type has always been a challenge. It took multiple iterations until we were able to find a good design and a robust implementation. I expect that hasManyThrough will be similar.

In general, each relation type needs the following artifacts:

  1. A repository interface & implementation providing access to related model instances, constraining the target models to enforce the relation. See e.g. has-many.repository.ts

  2. A decorator (typically for model properties) to specify relation metadata. See e.g. has-many.decorator.ts.

  3. A repository factory that accepts the value of the foreign key and returns back the relation repository. See e.g. HasManyRepositoryFactory. https://github.com/strongloop/loopback-next/blob/f015845f60649bddfa22bdd364154ebcf029a37b/packages/repository/src/relations/has-many/has-many-repository.factory.ts#L21-L23

  4. A function to create repository factory from relation metadata. See e.g. createHasManyRepositoryFactory. https://github.com/strongloop/loopback-next/blob/f015845f60649bddfa22bdd364154ebcf029a37b/packages/repository/src/relations/has-many/has-many-repository.factory.ts#L38-L57

  5. A protected helper method in DefaultCrudRepository making it easier to setup repository factory for a named relation. See e.g. _createHasManyRepositoryFactoryFor. https://github.com/strongloop/loopback-next/blob/f015845f60649bddfa22bdd364154ebcf029a37b/packages/repository/src/repositories/legacy-juggler-bridge.ts#L172-L185

I think the steps 2-5 should be relatively straightforward because they will mostly copy the design we already have in place for "hasMany" relation. I am expecting the first step to be most challenging, as we need to find out how to implement data access operations leveraging a "through" table.

Process wise, I am proposing to start with a spike on the first step. Start designing a Repository interface and implementation that will provide the following APIs:

  • "create" a new related model
  • "find" all related models
  • "add" an existing model into relation
    Other methods like "delete" and "update" should be easy to add later, once we agree on the overall design & implementation strategy.

Use integration tests to test the implementation. See relation.factory.integration.ts for inspiration, but please start a new file instead.

Open a pull request as soon as you have the first method ("create"?) implemented, so that we can discuss the design & implementation early on, while there is little code written and changes are easy to make.

@clayrisser
Copy link
Contributor

Thanks @bajtos, I'll see what I can do.

@dhmlau dhmlau added the Relations Model relations (has many, etc.) label Nov 28, 2018
@bajtos bajtos changed the title many to many relation in lb4 ?? Many to many relation (hasAndBelongsToMany) Dec 11, 2018
@bajtos bajtos added the feature label Dec 11, 2018
@RaphaelDrai
Copy link

Hello @bajtos,
I am happy to contribute with the attached design document related to the implementation of hasAndBelongsToMany relation in LB4.
I am looking forward for your kind review, comments of the design document and the directives how to make progress of this document before starting the implementation contribution.

Design_HasAndBelongsToMany_Contrib.docx

@bajtos
Copy link
Member

bajtos commented Dec 14, 2018

I'll be mostly away from my computer until the end of the year, I'll take a look in January. Merry Christmas! 🎄

@RaphaelDrai
Copy link

Merry Christmas!

@RaphaelDrai
Copy link

Hello @bajtos,
It is important for us to have in LB4 the hasAndBelongsToMany (the direct many-to-many relation) that we are using in our LB3 application. We are actually in a transition process to move our LB3 application to LB4 and hasAndBelongsToMany is considered as an high priority.
May I suggest to have a new and separated case for the hasManyThrough in order to avoid confusion with this current case?

@RaphaelDrai
Copy link

Hi @bajtos,
Is it possible to you to get your comment/advice on the design document that I sent one month ago?
Thanks,
Raphael

@RaphaelDrai
Copy link

@dhmlau, @raymondfeng , @gczobel-f5, @bajtos
Hello, I have a question please related to hasAndBelongsToMany relation database. What is the concept in loopback for creating the relation? does the user need to create an intermediate table that defines the many-to-many relation? If not how loopback is supposed to create the relation? Thanks a lot, Raphael

Raymond Feng [6:56 PM]
See https://guides.rubyonrails.org/association_basics.html#the-has-and-belongs-to-many-association

gczobel [7:21 PM]
@Raymond Feng The question here is intermediate table need to be generated by the user or loopback automatically will generate a new model-repository automatically for the relationship (assembly_parts) in the example

bajtos [11:11 AM]
I think LB4 should support both options:

  1. Initial experience: LB4 creates the intermediate model and repository under the hood.
  2. Advanced: the developer can supply their own intermediate model and repository to be used by HasAndBelongsToMany relation.

Implementation-wise, it may be easier to start with the advanced option, and once this is landed, then we can look into ways how to automate the creation of the intermediate model & repository.

Raphael Drai [10:58 AM]
Ok, I will start first with with option 2 "Advanced". I will make change in the design document that I sent then I will re-send it for review before starting the development. Thanks a lot.

@dhmlau
Copy link
Member

dhmlau commented Jan 30, 2019

For hasAndBelongsToMany relation type, please continue the discussion at #2308.

@CharlesIrvineKC
Copy link

I hope this is an appropriate place to post this question.... Is there a workaround for implementing many-to-many relationships in Loopback 4.? I think there must be since just about any significant database model will contain some number of many-to-many relationships. Without official support or a good workaround, I doubt that Loopback 4 would have been deemed ready to release otherwise. Thanks

@clayrisser
Copy link
Contributor

clayrisser commented Apr 15, 2019

The HasManyThrough pull request is currently functional. There's still more work to do on it to make it more efficient with SQL, but it does work.

#2359

The code for the pull request can be found at the following link.

https://github.com/codejamninja/loopback-next/tree/codejamninja/has-many-through-using-has-many

You can install it by running the following . . .

npm install --save https://github.com/codejamninja/loopback-next/releases/download/%40loopback%2Frepository-has-many-through-using-has-many/loopback-repository-has-many-through-using-has-many-1.2.1.tgz

. . . or by adding the following to the dependencies section in your package.json

    "@loopback/repository": "https://github.com/codejamninja/loopback-next/releases/download/%40loopback%2Frepository-has-many-through-using-has-many/loopback-repository-has-many-through-using-has-many-1.2.1.tgz"

@clayrisser
Copy link
Contributor

@clayrisser
Copy link
Contributor

clayrisser commented Apr 15, 2019

For hasManyThrough, you have to explicitly define your own join (junction) table. It's very similar to the way it works in loopback 3. You can read more about it at the following link.

https://loopback.io/doc/en/lb3/HasManyThrough-relations.html

In the example I provided, the junction table is defined by the Order model. (Notice the two @belongsTo decorators.)

https://github.com/codejamninja/loopback-next/blob/codejamninja/has-many-through-using-has-many/packages/repository/src/__tests__/fixtures/models/order.model.ts#L30-L34

Also, notice the way Customers and Sellers are linked together through an Order with the @hasMany decorator.

https://github.com/codejamninja/loopback-next/blob/codejamninja/has-many-through-using-has-many/packages/repository/src/__tests__/fixtures/models/seller.model.ts#L24-L25

https://github.com/codejamninja/loopback-next/blob/codejamninja/has-many-through-using-has-many/packages/repository/src/__tests__/fixtures/models/customer.model.ts#L27-L28

Since this feature has not been accepted into master yet, please let me know if you have any questions or bugs.

@clayrisser
Copy link
Contributor

clayrisser commented Apr 15, 2019

Like existing relationships in Loopback 4, you will need to wire it together in the repository.

For the hasManyThrough relationship, you use the HasManyThroughRepositoryFactory. You can see how sellers are linked to customers in the customer repository at the links below.

https://github.com/codejamninja/loopback-next/blob/codejamninja/has-many-through-using-has-many/packages/repository/src/__tests__/fixtures/repositories/customer.repository.ts#L41-L45

https://github.com/codejamninja/loopback-next/blob/codejamninja/has-many-through-using-has-many/packages/repository/src/__tests__/fixtures/repositories/customer.repository.ts#L73-L77

@charlypeluch
Copy link

@codejamninja great work!
Is it possible to create a repository with a simple example of 3 models implementing a many-to-many relationship?

Thank!

@clayrisser
Copy link
Contributor

Yes, absolutely. Look at the example below.

https://github.com/codejamninja/medical-practice-api

Currently, the physician repository isn't complete, but to complete it, it would simply be implemented in an identical fashion to the patient repository.

Feel free to submit a pull request to finish the patient repository.

@charlypeluch
Copy link

Yes, absolutely. Look at the example below.

https://github.com/codejamninja/medical-practice-api

Currently, the physician repository isn't complete, but to complete it, it would simply be implemented in an identical fashion to the patient repository.

Feel free to submit a pull request to finish the patient repository.

Hi @codejamninja , I'm using your example with relational databases and I notice that when I generate the database schema, the many-to-many relationship is not generated with foreignkey, there is some way in which this relationship is defined when migrating the schema ?

@dakt
Copy link

dakt commented May 9, 2019

Any dates on when will this be merged?

@clayrisser
Copy link
Contributor

@dakt I have to go through the comments and make some changes before the team can merge it. I will hopefully get around to it sometime this month. I have a feeling there will probably be around 3 pull requests before hasAndBelongsToMany is working the way it is intended to work.

@AzeemMichael
Copy link

is loopback 4 not ready for production use? it still seems to be missing this

@clayrisser
Copy link
Contributor

@AzeemMichael if your application needs a many to many relationship, then yes it's probably not production ready. However I have built several loopback 4 apps that are in production that do not use the many to many relationship.

@ramymaghraby
Copy link

I find loopback 4 is one the greatest frameworks, however the many to many relation (HasManyThrough) is very essential to most of the work i intend to use loopback for, hope this be released soon.

@MacGyver26
Copy link

Hello guys !

Do you know when this feature is going to be available in the framework ? thanks in advance.

@stale
Copy link

stale bot commented Dec 25, 2020

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 stale bot added the stale label Dec 25, 2020
@agnes512 agnes512 removed the stale label Jan 13, 2021
@stale
Copy link

stale bot commented Jul 14, 2021

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 stale bot added the stale label Jul 14, 2021
@jasknky
Copy link

jasknky commented Jul 29, 2021

hello !!
it's been over more than an year since the last comments :
the hasAndBelongsToMany relation will be merged in the future or not?

@stale stale bot removed the stale label Jul 29, 2021
@welchsteven
Copy link

It looks like there are several duplicate issues for the same request. Another open one is here #2308. Also, it looks like @frbuceta actually finished some code in #6452. Any chance we can get this feature merged soon? If there is something I can do to move this feature forward please let me know.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature Relations Model relations (has many, etc.)
Projects
Status: Icebox
Development

Successfully merging a pull request may close this issue.