gpt4 book ai didi

node.js - TypeError : include. model.getTableName 不是函数

转载 作者:行者123 更新时间:2023-12-03 22:32:26 24 4
gpt4 key购买 nike

他认为表之间的关系有问题。每个用户最多可以有一份实习申请。
但是,当我想从这些应用程序访问用户时,遇到了TypeError: include.model.getTableName is not a function 问题,我仍然没有解决。
我必须将 2 个模型相互结合。
用户模型

 const User = sequelize.define('user', {

mail: {
allowNull: false,
type: DataTypes.STRING,
unique: {
args: true,
msg: 'Bu mail adresi kullanılmaktadır !',
},
validate: {
notEmpty: {
args: true,
msg: 'Bu alan boş bırakılamaz !',
},
isEmail: {
args: true,
msg: 'Kurallara uygun mail adresi giriniz !',
},
}
},

password: {
allowNull: false,
type: DataTypes.STRING(64),
validate: {
len: {
args: [8, 64],
msg: 'Şifre en az 8 karakterden oluşabilir.'
},
}
}


}, {
tableName: "users",

});


User.associate = (models) => {

User.hasOne(models.userrole, {
as: 'userroles',
foreignKey: 'userID'
});
User.belongsTo(models.internship, {
as: 'internships',
foreignKey: 'id'
});
};
实习模式
 const Internship = sequelize.define('internship', {

userID: {
type: DataTypes.INTEGER,
allowNull: false,
},
userInfoID: {
type: DataTypes.INTEGER,
allowNull: false,
},
schoolInfoID: {
type: DataTypes.INTEGER,
allowNull: false,
},
privateInfoID: {
type: DataTypes.INTEGER,
allowNull: false,
},
statusID: {
type: DataTypes.INTEGER,
allowNull: false,

},
}, {
tableName: "internships"
});

Internship.associate = (models) => {

Internship.belongsTo(models.user, {
as: 'users',
foreignKey: 'userID'
});
};

Controller
     internship.findAll({ 
include: [
{
as:"users",
model: user,

},
]
}).then(result=>{
res.status(200).json({
result })
})

最佳答案

您的 Internship 模型和用户模型中没有定义任何主键,并且关联中也存在模型拼写错误。

const User = sequelize.define('user', {
id:{
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
mail: {
allowNull: false,
type: DataTypes.STRING,
unique: {
args: true,
msg: 'Bu mail adresi kullanılmaktadır !',
},
validate: {
notEmpty: {
args: true,
msg: 'Bu alan boş bırakılamaz !',
},
isEmail: {
args: true,
msg: 'Kurallara uygun mail adresi giriniz !',
},
}
},

password: {
allowNull: false,
type: DataTypes.STRING(64),
validate: {
len: {
args: [8, 64],
msg: 'Şifre en az 8 karakterden oluşabilir.'
},
}
}


}, {
tableName: "users",

});


User.associate = (models) => {

User.hasOne(models.userrole, {
as: 'userroles',
foreignKey: 'userID'
});
User.belongsTo(models.Internship, {
as: 'internships',
foreignKey: 'id'
});
};

实习模式

const Internship = sequelize.define('internship', {
id:{
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
userID: {
type: DataTypes.INTEGER,
allowNull: false,
},
userInfoID: {
type: DataTypes.INTEGER,
allowNull: false,
},
schoolInfoID: {
type: DataTypes.INTEGER,
allowNull: false,
},
privateInfoID: {
type: DataTypes.INTEGER,
allowNull: false,
},
statusID: {
type: DataTypes.INTEGER,
allowNull: false,

},
}, {
tableName: "internships"
});

Internship.associate = (models) => {

Internship.belongsTo(models.User, {
as: 'users',
foreignKey: 'userID'
});
};

Controller

     Internship.findAll({ 
include: [
{
as:"users",
model: User,

},
]
}).then(result=>{
res.status(200).json({
result })
})

关于node.js - TypeError : include. model.getTableName 不是函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65598223/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com