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

Typescript example of M:N relationship (belongsToMany) with through table attributes #11644

Open
2 of 7 tasks
hburrows opened this issue Nov 5, 2019 · 7 comments
Open
2 of 7 tasks
Labels
type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.

Comments

@hburrows
Copy link

hburrows commented Nov 5, 2019

Issue Description

Examples of using Typescript with many-to-many relationships (i.e. belongsToMany <-> belongsToMany)

What was unclear/insufficient/not covered in the documentation

Only simple relationship examples exist: i.e hasMany

Additional context

I've looked through the type definitions and tests and I'm coming up empty-handed.

Simple example:

class Account extends Model { }

class User extends Model {
  public addAccount!: ????
}

User.belongsToMany(Account, { through: AccountUser });
Account.belongsToMany(User, { through: AccountUser });

userInst.addAccount(accountInst, { through: { role: 'owner' } })

How do I declare the addAccount method of class User? My through table (AccountUser) has a single attribute (role). I've looked through the code (i.e. associations/belongs-to-many.d.ts) but I'm stumped. It appears that BelongsToManyAddAssociationMixin takes 3 arguments but when I express something like:

public addAccount!: BelongsToManyAddAssociationMixin<Account, string, { role: string }>;

it complains with Generic type 'BelongsToManyAddAssociationMixin' requires 2 type argument(s)

Some help and guidance would be greatly appreciated. I'd be happy to contribute to the Typescript documentation page to cover this use case.

Is this issue dialect-specific?

  • No. This issue is relevant to Sequelize as a whole.
  • Yes. This issue only applies to the following dialect(s): Typescript
  • I don't know.

Would you be willing to resolve this issue by submitting a Pull Request?

  • Yes, I have the time and I know how to start.
  • Yes, I have the time but I don't know how to start, I would need guidance.
  • No, I don't have the time, although I believe I could do it if I had the time...
  • No, I don't have the time and I wouldn't even know how to start.
@hburrows hburrows added the type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. label Nov 5, 2019
@papb
Copy link
Member

papb commented Nov 10, 2019

Hello!

userInst.addAccount(accountInst, { throught: { role: 'owner' } })

There is a typo here, throught -> through

@papb papb added status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense. labels Nov 10, 2019
@hburrows
Copy link
Author

@papb Thanks for pointing that out. I fixed it. I finally figured out how to make it work. More examples would be good, however.

@papb
Copy link
Member

papb commented Nov 12, 2019

I finally figured out how to make it work.

Nice!!

More examples would be good, however.

I agree 😬 Would you be willing to post how did you finally make it work?

@Crodaycat
Copy link

Hello, can somebody please show an example project to reach M:N realtionships.

Thanks.

@Reinoptland
Copy link

Hey @Crodaycat & others

I am also working on my first Sequelize / Typescript selfstudy project. I found this example to be pretty useful (though it is from 2 years ago)

https://github.com/ahmerb/typescript-sequelize-example

@github-actions
Copy link
Contributor

github-actions bot commented Nov 5, 2021

This issue has been automatically marked as stale because it has been open for 7 days without activity. It will be closed if no further activity occurs. If this is still an issue, just leave a comment or remove the "stale" label. 🙂

@github-actions github-actions bot added the stale label Nov 5, 2021
@WikiRik WikiRik removed the stale label Nov 15, 2021
@Taqiarzoo
Copy link

Taqiarzoo commented Jul 12, 2022

there is very less documentation for sequlize typescript
i spend hours to figure out how to disable unique constrain on through table,
here is the code snippet

Design.belongsToMany(File, { through: { model: FileUsage, unique: false }, as: 'design_files', foreignKey: Design_.DESIGNID, otherKey: GlobalFile.FILEID })

File.belongsToMany(Design, { through: { model: FileUsage, unique: false }, as: 'design_files', foreignKey: GlobalFile.FILEID, otherKey: Design_.DESIGNID })

but i still don't know how to do the same with this syntax

@BelongsToMany(() => Design, { through: () => RelatedDesign, as: 'design' })

@github-actions github-actions bot removed the status: awaiting response For issues and PRs. OP must respond (or change something, if it is a PR). Maintainers have no action label Jul 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: docs For issues and PRs. Things related to documentation, such as changes in the manuals / API reference. type: typescript For issues and PRs. Things that involve typescript, such as typings and intellisense.
Projects
None yet
Development

No branches or pull requests

6 participants