他认为表之间的关系有问题。每个用户最多可以申请一份实习机会。 但是,当我想从这些应用程序访问用户时,遇到了
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'
});
};
控制器
internship.findAll({
include: [
{
as:"users",
model: user,
},
]
}).then(result=>{
res.status(200).json({
result })
})
您没有在实习模型和用户模型中定义任何主键,并且关联中也存在模型拼写错误。
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'
});
};
控制器
Internship.findAll({
include: [
{
as:"users",
model: User,
},
]
}).then(result=>{
res.status(200).json({
result })
})
这是一个延迟回复,但我将其留在这里以供将来搜索。
官方文档(v6-stable)提供了示例
include: [{ model: 'table_name' }]`
但是这些都不起作用。使用数组时,
includes : [{ model: 'table_name' }]
应该使用
来代替。