简体   繁体   English

node js sequelize 在 model 中定义一个字段,类型为另一个 model

[英]node js sequelize define a field inside model with type of another model

i tried to import the model and give the field the type of the model but i get error that its not assignable.我尝试导入 model 并为该字段提供 model 的类型,但我得到错误,它不可分配。

 import { User } from './'; followerId: { type: User, allowNull: false, }

(btw i know you can get the field with relations but i need another field with the same type) (顺便说一句,我知道您可以获得具有关系的字段,但我需要另一个具有相同类型的字段)

To achieve that you need an association:为此,您需要一个关联:

https://sequelize.org/master/manual/assocs.html https://sequelize.org/master/manual/assocs.html

YourModel.hasOne(User, {
    foreignKey: {
      name: 'followerId'
    }
});
User.belongsTo(YourModel);

and use include in queries:并在查询中使用include

const result = await YourModel.find({
  where: {
    // ...
  },
  include: User
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM