title: Sequelize Typescript many-to-many specify 'through' Model attributes
body: <p>I am using sequelize-typescript and trying to not include the 'through' model (UserRole) in the many-to-many association when a get a User.</p>
  <p>When I get a user, the JSON looks like this:</p>
  <pre><code>{
      &quot;id&quot;: 1,
      &quot;email&quot;: &quot;&quot;,
      &quot;password&quot;: &quot;&quot;,
      &quot;isVerified&quot;: true,
      &quot;verificationToken&quot;: &quot;&quot;,
      &quot;passwordResetToken&quot;: null,
      &quot;createdAt&quot;: &quot;2022-02-17T03:04:34.099Z&quot;,
      &quot;updatedAt&quot;: &quot;2022-02-17T03:05:20.754Z&quot;,
      &quot;roles&quot;: [
          {
              &quot;name&quot;: &quot;SUPERADMIN&quot;,
              &quot;UserRole&quot;: {
                  &quot;userId&quot;: 1,
                  &quot;roleId&quot;: 2,
                  &quot;createdAt&quot;: &quot;2022-02-17T03:05:32.474Z&quot;,
                  &quot;updatedAt&quot;: &quot;2022-02-17T03:05:32.474Z&quot;
              }
          }
      ]
  }
  </code></pre>
  <p>Ideally, the &quot;UserRole&quot; property in the &quot;roles&quot; array would not be there. Or, at the very least, I would be able to change the name from &quot;UserRole&quot; to &quot;userRole&quot; and remove the &quot;userId&quot; and &quot;roleId&quot; attributes.</p>
  <p>I have tried specifying the &quot;as&quot; property on the association, creating default scopes for each of the model, all to no avail. This person <a href="https://stackoverflow.com/questions/45130037/sequelize-typescript-many-to-many-relationship-model-data-with">sequelize-typescript many-to-many relationship model data with</a> had a similar issue, but was concerned with the HasMany relationship, where I am looking at the BelongsToMany relationship. My model files look like this:</p>
  <p>user.model.ts</p>
  <pre><code>import {
    Table,
    Column,
    Model,
    DataType,
    HasMany,
    BelongsToMany,
    DefaultScope,
  } from &quot;sequelize-typescript&quot;;
  import { RefreshToken } from &quot;./refreshToken.model&quot;;
  import { Role } from &quot;./role.model&quot;;
  import { UserRole } from &quot;./userRole&quot;;
  
  @DefaultScope(() =&gt; ({
    include: [{ model: Role, attributes: [&quot;name&quot;], include: [] }],
  }))
  @Table({ tableName: &quot;user&quot;, underscored: true })
  class User extends Model {
    @Column(DataType.STRING)
    email!: string;
  
    @Column(DataType.STRING)
    password!: string;
  
    @Column(DataType.BOOLEAN)
    isVerified!: boolean;
  
    @Column(DataType.STRING)
    verificationToken!: string;
  
    @Column(DataType.STRING)
    passwordResetToken!: string;
  
    @HasMany(() =&gt; RefreshToken, {
      onDelete: &quot;CASCADE&quot;,
    })
    refreshTokens!: RefreshToken[];
  
    @BelongsToMany(() =&gt; Role, {
      through: { model: () =&gt; UserRole },
    })
    roles!: Role[];
  
    @HasMany(() =&gt; UserRole, {
      onDelete: &quot;CASCADE&quot;,
    })
    userRoles!: UserRole[];
  }
  
  export { User };
  </code></pre>
  <p>role.model.ts</p>
  <pre><code>import {
    Table,
    Column,
    Model,
    DataType,
    HasMany,
    BelongsToMany,
  } from &quot;sequelize-typescript&quot;;
  import { User } from &quot;./user.model&quot;;
  import { UserRole } from &quot;./userRole&quot;;
  
  @Table({ tableName: &quot;role&quot;, underscored: true, timestamps: false })
  class Role extends Model {
    @Column(DataType.STRING)
    name!: &quot;ADMIN&quot; | &quot;SUPERADMIN&quot;;
  
    @BelongsToMany(() =&gt; User, {
      through: { model: () =&gt; UserRole },
    })
    users!: User[];
  
    @HasMany(() =&gt; UserRole, {
      onDelete: &quot;CASCADE&quot;,
    })
    userRoles!: UserRole[];
  }
  
  export { Role };
  </code></pre>
  <p>userRole.model.ts</p>
  <pre><code>import {
    Table,
    Model,
    ForeignKey,
    BelongsTo,
    PrimaryKey,
    Column,
  } from &quot;sequelize-typescript&quot;;
  import { Role } from &quot;./role.model&quot;;
  import { User } from &quot;./user.model&quot;;
  
  @Table({ tableName: &quot;user_role&quot;, underscored: true })
  class UserRole extends Model {
    @BelongsTo(() =&gt; User)
    user!: User;
  
    @ForeignKey(() =&gt; User)
    @PrimaryKey
    @Column
    userId!: number;
  
    @BelongsTo(() =&gt; Role)
    role!: Role;
  
    @ForeignKey(() =&gt; Role)
    @PrimaryKey
    @Column
    roleId!: number;
  }
  
  export { UserRole };
  </code></pre>