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 Relationship #224

Open
blalan05 opened this issue Aug 14, 2023 · 7 comments
Open

Many-to-Many Relationship #224

blalan05 opened this issue Aug 14, 2023 · 7 comments

Comments

@blalan05
Copy link

blalan05 commented Aug 14, 2023

Is your feature request related to a problem? Please describe.
I'd like to create complex relationship with through tables.

User
id
name
UserToType
userId
userTypeId
UserType
id
name

Describe the solution you'd like
Add the ability to define Many-to-Many relationships

Describe alternatives you've considered
I think its possible to define through table manually, then use the relationship definition like One-to-Many.

Additional context
Add any other context or screenshots about the feature request here.

@kk7771
Copy link

kk7771 commented Sep 14, 2023

Is there any progress? I also encountered the same problem.
@noam-honig

@noam-honig
Copy link
Collaborator

Hi @balan05 & @Vincent1016 - sorry for not replying earlier

In the current version of remult, you can do that with a 'through' table

@Entity('users')
class User {
  @Fields.cuid()
  id = ''
  @Fields.string()
  name = ''
}
@Entity('UserTypes')
class UserType {
  @Fields.cuid()
  id = ''
  @Fields.string()
  name = ''
}
@Entity<UserToType>('UsersToTypes', {
  id: (e) => [e.userId, e.typeId],
})
class UserToType {
  @Fields.string()
  userId = ''
  @Fields.string()
  typeId = ''
}

You can employ advanced filtering techniques as detailed in:
https://remult.dev/docs/techniques-regarding-one-to-many-relations.html

You can also add many to one relations to the mix - but in that case, at the current version, I recommend adding a separate id column for good measure

@Entity<UserToType>('UsersToTypes')
class UserToType {
  @Fields.cuid()
  id = ''
  @Field(() => User, { lazy: true //to only load when requested 
    })
  userId!: User
  @Field(() => UserType)
  typeId!: UserType
}

You can use lazy to control if the other row is loaded by default or not - see:
https://remult.dev/docs/lazy-loading-of-related-entities.html#lazy-loading-of-related-entities

We are currently planning some improvements in the relations area, stay tuned

@kk7771
Copy link

kk7771 commented Sep 14, 2023

@noam-honig
good job。 remult is great.

@mihaa1
Copy link

mihaa1 commented Oct 18, 2023

Hi @balan05 & @Vincent1016 - sorry for not replying earlier

In the current version of remult, you can do that with a 'through' table

@Entity('users')
class User {
  @Fields.cuid()
  id = ''
  @Fields.string()
  name = ''
}
@Entity('UserTypes')
class UserType {
  @Fields.cuid()
  id = ''
  @Fields.string()
  name = ''
}
@Entity<UserToType>('UsersToTypes', {
  id: (e) => [e.userId, e.typeId],
})
class UserToType {
  @Fields.string()
  userId = ''
  @Fields.string()
  typeId = ''
}

You can employ advanced filtering techniques as detailed in: https://remult.dev/docs/techniques-regarding-one-to-many-relations.html

You can also add many to one relations to the mix - but in that case, at the current version, I recommend adding a separate id column for good measure

@Entity<UserToType>('UsersToTypes')
class UserToType {
  @Fields.cuid()
  id = ''
  @Field(() => User, { lazy: true //to only load when requested 
    })
  userId!: User
  @Field(() => UserType)
  typeId!: UserType
}

You can use lazy to control if the other row is loaded by default or not - see: https://remult.dev/docs/lazy-loading-of-related-entities.html#lazy-loading-of-related-entities

We are currently planning some improvements in the relations area, stay tuned

Hey,
Related - when using relations as in docs - should an underlying foreign key be created?
Im using postgres - either I'm missing something or it is not created

@noam-honig
Copy link
Collaborator

Hi @mihaa1

No, at this time we do not generate a foreign key in the db by default - we only generate the structure to store the data, and leave it to the developer to create the meta data objects such as indexes, foreign keys etc...

You can easily do that as part of the version update process discussed in:
#162

@mihaa1
Copy link

mihaa1 commented Oct 19, 2023

Hi @mihaa1

No, at this time we do not generate a foreign key in the db by default - we only generate the structure to store the data, and leave it to the developer to create the meta data objects such as indexes, foreign keys etc...

You can easily do that as part of the version update process discussed in: #162

Thanks.
Rules of relations are enforced by code then inside remult?
e.g. creating relation with non existent id

@noam-honig
Copy link
Collaborator

noam-honig commented Oct 19, 2023 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants