'Sequelize-typescript create with per-existing association via through table
I am trying to create a new role entry by using the sequelize create method via a through "table" called RolesPermissions and connecting it to a preexisting permission. What it actually does is its trying to INSERT the permission(Although it already exists.) I'm not sure if this is a configuration issue on my side. NOTE: Each role can have many permissions and a permission can belong to many roles(ManyToMany)
Role class:
@Table
export default class Roles extends CreatedUpdateModel {
@AllowNull(false)
@Unique(true)
@Column
name: string;
@AllowNull(false)
@Column
description: string;
@BelongsToMany(() => Users, () => UsersRoles)
users: Array<Users & { UsersRoles: UsersRoles }>;
@BelongsToMany(() => Permissions, () => RolesPermissions)
permissions: Array<Permissions & { RolesPermissions: RolesPermissions }>;
}
Permission class:
@Table({
timestamps: false,
})
export default class Permissions extends Model {
@AllowNull(false)
@Column
name: string;
@Column
remarks: string;
@BelongsToMany(() => Roles, () => RolesPermissions)
roles: Array<Roles & { RolesPermissions: RolesPermissions }>;
}
RolesPermission class (through table)
@Table({
timestamps: false,
})
export default class RolesPermissions extends Model {
@AllowNull(false)
@ForeignKey(() => Roles)
@Column
roleId: number;
@AllowNull(false)
@ForeignKey(() => Permissions)
@Column
permissionId: number;
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
