我有两个模型,但我不知道为什么会出现以下错误:
Unhandled rejection SequelizeEagerLoadingError: user is not associated to userPatientAssociation
第一个模型是user
,第二个模型是userPatintAssociation
。
不包括第三种模型patient
。我不知道我是否想念什么。
我尝试了关联,但是抛出此错误。
user
模型:const { sequelize, Sequelize, Model } = require('../config/db');
const userPatientAssociation = require('../models/userPatientAssociation');
class User extends Model { }
User.init({
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
validate: {
isNumeric: true,
},
},
practiceId: {
type: Sequelize.STRING,
allowNull: true,
field: 'practice_id',
unique: true,
},
username: {
type: Sequelize.STRING,
allowNull: false,
field: 'username',
unique: true,
},
token: {
type: Sequelize.STRING,
field: 'token',
},
refreshToken: {
type: Sequelize.STRING,
field: 'refresh_token',
},
name: {
type: Sequelize.STRING,
allowNull: false,
field: 'name',
},
email: {
type: Sequelize.STRING,
allowNull: false,
field: 'email',
},
mobileNo: {
type: Sequelize.STRING,
allowNull: false,
field: 'mobile_no',
},
password: {
type: Sequelize.STRING,
allowNull: false,
field: 'password',
},
createdAt: {
type: Sequelize.DATE,
field: 'created_at',
},
},
{
sequelize,
modelName: 'user',
});
module.exports = User;
association
模型:const user = require('../models/user');
const patient = require('../models/patient');
const { sequelize, Sequelize, Model } = require('../config/db');
class UserPatientAssociation extends Model {}
UserPatientAssociation.init({
id: {
type: Sequelize.INTEGER,
primaryKey: true,
autoIncrement: true,
validate: {
isNumeric: true,
},
},
userId: {
type: Sequelize.INTEGER,
field: 'user_id',
validate: {
isNumeric: true,
},
references: {
model: user,
key: 'id',
},
},
previousUserId: {
type: Sequelize.INTEGER,
field: 'previous_user_id',
validate: {
isNumeric: true,
},
references: {
model: user,
key: 'id',
},
},
patientId: {
type: Sequelize.INTEGER,
field: 'patient_id',
references: {
model: patient,
key: 'id',
},
},
},
{
sequelize,
modelName: 'userPatientAssociation',
});
module.exports = UserPatientAssociation;