question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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

See original GitHub issue

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.

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
Reinoptlandcommented, Jul 4, 2020

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

1reaction
papbcommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Sequelize Typescript many-to-many specify 'through' Model ...
Sequelize Typescript many-to-many specify 'through' Model attributes ; { Table, Column, Model, DataType, HasMany, BelongsToMany, DefaultScope, } ...
Read more >
Advanced M:N Associations | Sequelize
We established a Many-to-Many relationship between User and Profile using the Grant model as the through table: User.belongsToMany(Profile, { ...
Read more >
Sequelize Typescript many-to-many specify 'through' Model ...
When you include a ManyToMany relationship, include directly the final table and not the through table. So if you want to interact with...
Read more >
Sequelize Many-to-Many Association example - Node.js ...
Way to implement Sequelize Associations example: many to many Relationship in Node.js - Sequelize Associate tutorial with example.
Read more >
How to use the Sequelize belongsToMany() method with code ...
INSERT new rows to tables with a Many-To-Many relationship ... You need to pass the attributes of the associated table and use the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found