Sequelize parentModel.AddChild 创建记录,其中 parentModel 为 child

Sequelize parentModel.AddChild creates record where parentModel as child

我已经配置了table与自身的多对多关系。这是实现“接口”的table - “我可以有一个概述,可审查”

///HasReviewModel

static associate(models) {
            // define association here
            this.belongsToMany(models.HasReview, {
                through: models.HasReviewHasReview,
                as: 'child',
                foreignKey: 'childId',
                onDelete: 'CASCADE',
                onUpdate: 'CASCADE',
            });
    
            this.belongsToMany(models.HasReview, {
                through: models.HasReviewHasReview,
                as: 'parent',
                foreignKey: 'parentId',
                onDelete: 'CASCADE',
                onUpdate: 'CASCADE',
            });
            
            
            this.hasMany(models.Like, {onDelete: "cascade", onUpdate: "cascade", foreignKey: {allowNull: false}});
        }

另外,在代码中,我调用addChild()在linktable中添加了一条记录,其中小说(post)会有评论(评论) .

async writeReview(novelId, userId, text) {
        const novel = await db.Novel.findOne({
            where: {id: novelId},
            include: [{model: db.HasReview}]
        });
    
        let transaction;
        try {
            // get transaction
            transaction = await db.sequelize.transaction();
    
            const newReviewInterface = await db.HasReview.create({});
    
            const review = await db.Review.create({
                text: text,
                hasReviewId: newReviewInterface.id,
                userId: userId
            });
    
            //AddChild - novel added like child
            novel.hasReview.addChild(newReviewInterface, {
                through: {status: 'review'}
            });
            
            // AddParent - novel added like child too
            // newReviewInterface.addParent(novel.hasReview, {
            //     through: {status: 'review'}
            // });
            
            if (!review)
                throw new Error(`Не удалось создать комментарий, отмена транзакции`);
            
            // commit
            await transaction.commit();
            
            return review;

但出于某种原因,它的工作方式正好相反。亲子互换位置

数据库记录: https://i.stack.imgur.com/dI42m.png

如果我们调用addParent方法,它会再次反转并创建我需要的记录。 为什么?以及如何使其正常工作?我哪里错了?

P.S。摆在我面前的任务是制定一种通用的方式来评论任何实体。 无论是小说、post 还是其他评论。

发现问题! 我在错误的 belongsToMany 调用中声明了一个外键。 以下是正确的条目

///HasReviewModel

static associate(models) {
            // define association here
            this.belongsToMany(models.HasReview, {
                through: models.HasReviewHasReview,
                as: 'child',
                foreignKey: 'parentId', //HERE
                onDelete: 'CASCADE',
                onUpdate: 'CASCADE',
            });
    
            this.belongsToMany(models.HasReview, {
                through: models.HasReviewHasReview,
                as: 'parent',
                foreignKey: 'childId', //HERE
                onDelete: 'CASCADE',
                onUpdate: 'CASCADE',
            });

外键是为来自 dependent、从属 table 的列设置的,并指向来自 main[=] 的列之一21=] table.

我们说 - 像 child 一样加入“THIS”table,您的 foreignKey 将成为 parentId。 此 table 中指向外部的键。