m2m关联未加载直通表[sequelize]上的字段

gkl3eglg  于 2021-09-23  发布在  Java
关注(0)|答案(0)|浏览(193)

我在sequelize上遇到了一个问题,我有3种不同的型号:

const School = sequelize.define('school', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true
    },
    name: Sequelize.STRING,
    description: Sequelize.STRING,
    private: Sequelize.BOOLEAN,
    createdAt: Sequelize.DATE,
    updatedAt: Sequelize.DATE
})

const SchoolMember = sequelize.define('schoolMember', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true
    },
    isAdmin: Sequelize.BOOLEAN,
    schoolId: {
        type: Sequelize.DataTypes.INTEGER,
        references: {
            model: 'communities',
            key: 'id'
        }
    },
    profileId: {
        type: Sequelize.DataTypes.INTEGER,
        references: {
            model: 'profiles',
            key: 'id'
        }
    },
    createdAt: Sequelize.DATE,
    updatedAt: Sequelize.DATE,
})

const Profile = sequelize.define('profile', {
    id: {
        type: Sequelize.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey: true
    },
    name: Sequelize.STRING,
    description: Sequelize.STRING,
    picture: Sequelize.STRING,
    createdAt: Sequelize.DATE,
    updatedAt: Sequelize.DATE
})

我使用schoolmember作为传递表,在school和profile之间建立了多对多关联,如下所示:

School.belongsToMany(Profile, {
        through: 'schoolMembers',
        as: 'members'
    })

现在,当我试图通过api获取我的学校时:

const school = await School.findOne({
        where: {
            id: schoolId
        },
        include: {
            all: true,
            nested: true
        }
    })

我用一个名为members的额外字段接收我的对象

members: Array(1)
0:
agreement: true
schoolMembers:
schoolId: 1
createdAt: "2021-07-26T18:13:00.210Z"
profileId: 1
updatedAt: "2021-07-26T18:13:00.210Z"
__proto__: Object

...
//Profile data...
...

但是它既不加载id也不加载isadmin值(我真的需要它),我在这里遗漏了什么?谢谢你的帮助

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题