类方法中的 Sequelize 关联不起作用 [英] Sequelize Association in class methods not working

查看:56
本文介绍了类方法中的 Sequelize 关联不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在类方法中设置关联,但它无法正常工作!

const User = sequelize.define('User', {电子邮件:DataTypes.STRING,密码:DataTypes.STRING,}, {类方法:{助理:(模型)=>{User.hasMany(models.AnotherModel, {foreignKey: 'userId'});},}});

但是当我在 classMethods 块之外设置关联时,它可以工作:

User.associate = 函数(模型){User.hasMany(models.AnotherModel, {foreignKey: 'userId'});};

为什么 classMethods 块中的代码不起作用?

续集版本:4.2.0

解决方案

这是一个预期的行为,classmethod 语法指的是 sequelize 版本 <4.

自 v4 以来,sequelize 已经朝着 js 类语法发展,因此发生了变化.

升级手册这里详细说明>

I'm trying to set associations in class methods but it's not working as it should!

const User = sequelize.define('User', {
    email: DataTypes.STRING,
    password: DataTypes.STRING,
}, {
    classMethods: {
        associate: (models) => {
            User.hasMany(models.AnotherModel, {foreignKey: 'userId'});
        },
   }
});

But when i set associations outside of classMethods block it works:

User.associate = function (models) {
    User.hasMany(models.AnotherModel, {foreignKey: 'userId'});
};

why the codes inside classMethods block not working?

sequelize version: 4.2.0

解决方案

This is an expected behavior, the classmethod syntax refers to sequelize version < 4.

Since v4, sequelize has evolved more towards js class syntax and hence the change.

The upgrade manual here details more about it

这篇关于类方法中的 Sequelize 关联不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆