Sequelize错误模型没有关联

时间:2015-10-10 10:33:00

标签: sequelize.js

我有两个模型:我在同步之前关联的userAuth和userFollow。 关联是userFollow有两个引用用户的外键。 我没有使用别名,但是当我这样做时:

userAuth.findOne({
        where:{
            id: "someid"
        },
        include: [{
            model: userFollow
        }],
        logging: false
    })

它会抛出错误,上面写着:[错误:user_follow与user_auth无关!]

我有这种类方法"关联"在我同步之前调用的userFollow中:

classMethods: {
            associate: function(models) {
                // ALTER TABLE user_follow
                // ADD FOREIGN KEY (follower_id)
                // REFERENCES user_auth(id)
                // ON DELETE CASCADE
                // ON UPDATE CASCADE
                userFollow.belongsTo(models.user_auth, {

                    onDelete: "CASCADE",

                    onUpdate: "CASCADE",

                    foreignKey: 'follower_id',

                    targetKey: 'id'
                });

                // ALTER TABLE user_follow
                // ADD FOREIGN KEY (follower_id)
                // REFERENCES user_auth(id)
                // ON DELETE CASCADE
                // ON UPDATE CASCADE
                userFollow.belongsTo(models.user_auth, {

                    onDelete: "CASCADE",

                    onUpdate: "CASCADE",

                    foreignKey: 'followee_id',

                    targetKey: 'id'
                });
            }
        },

0 个答案:

没有答案