将v3更新到v4:对关联不再有效

时间:2019-12-05 15:31:55

标签: mysql node.js express sequelize-typescript

由于我已将我的续集版本更新为4.28(以前为3),所以一个属于许多关联的网站似乎无法正常工作。

该表的名称为UserCapabilities(许多表)。在版本3中,通过addCapability方法触发了sql插入操作,但是在使用v4的情况下,不再需要sql插入操作。

感谢您的帮助

代码如下:

user.model.ts

export const UserCapabilityModel: models.user.capability.Model =
    db.define<models.user.capability.Instance, models.user.capability.Attributes>('UserCapability', {
    });

UserModel.belongsToMany(CapabilityModel, {
    through: UserCapabilityModel,
    foreignKey: 'userId',
    as: 'capabilities'
});

CapabilityModel.belongsToMany(UserModel, {
    through: UserCapabilityModel,
    foreignKey: 'capabilityId'
});

service.ts

await developmentUser.addCapability(capabilityId, { transaction: transaction });

user.d.ts

import * as Sequelize from 'sequelize';

declare module 'models' {

    export namespace models {
        namespace user {

            interface Attributes {
                id?: number;
                firstName?: string;
                lastName?: string;
                username?: string;
                email?: string;
                persNr?: string;
            }

            interface Instance extends Sequelize.Instance<Attributes>, Attributes {
                roles?: models.role.Instance[];
                team?: models.team.Instance;
                teamId?: number;
                managerId?: number;
                manager?: models.user.Instance[];
                capabilities?: models.capability.Instance[];

                addRole(args: string, options?: Sequelize.InstanceSaveOptions): Promise<Instance>;
                setRoles(keyss: string[], options?: Sequelize.InstanceSaveOptions): Promise<Instance>;
                getRoles(): Promise<models.role.Instance[]>;
                // Only for the oauth user
                hasRole(key: string): boolean;

                getTeam(): Promise<models.team.Instance>;
                setTeam(id: number): Promise<models.team.Instance>;
                getManager(): Promise<models.user.Instance>;
                setManager(id: number): Promise<models.user.Instance>;

                addCapability(id: number, options?: Sequelize.RestoreOptions): Promise<capability.Instance>;
                removeCapability(id: number, options?: Sequelize.RestoreOptions): Promise<capability.Instance>;
                setCapabilities(ids: number[], options?: Sequelize.RestoreOptions): Promise<capability.Instance[]>;
                getCapabilities(options?: Sequelize.FindOptions): Promise<models.capability.Instance[]>;
                getOnlyCapabilities(): Promise<models.capability.Instance[]>;

                getSkills(): Promise<models.capability.Instance[]>;
                getExpiringDevelopments(): Promise<models.development.Instance[]>;
            }

0 个答案:

没有答案