我已经阅读了文档并进行了大量研究,但我仍然找不到解决此问题的方法,有人可以给我一个提示吗?我究竟做错了什么?
错误:
C:\Users\Marlon\Desktop\Projeto\EuroKT\backend\node_modules\sequelize\lib\associations\mixin.js:49
throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model`);
^
Error: User.belongsToMany called with something that's not a subclass of Sequelize.Model
at Function.belongsToMany (C:\Users\Marlon\Desktop\Projeto\EuroKT\backend\node_modules\sequelize\lib\associations\mixin.js:49:13)
at Object.<anonymous> (C:\Users\Marlon\Desktop\Projeto\EuroKT\backend\dist\src\data\models\user.js:81:6)
用户类:
import { Model, DataTypes } from 'sequelize';
import { DbInstance } from '../../main/context';
import { Attributes } from '../../commons/Helpers';
import * as Config from '../../config.json';
import Permission from './permission';
import Project from './project';
var _reSync = Config.Database.ForceSync;
var _instance = new DbInstance().getInstance();
class User extends Model {
id!: number;
status: string;
name!: string;
registryCode!: string;
phone!: string;
email!: string;
password!: string;
permissionId!: number;
}
User.init({
id: {
type: new DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
status: {
type: new DataTypes.CHAR(2)
},
name: {
type: new DataTypes.STRING(30),
allowNull: false
},
registryCode: {
type: new DataTypes.STRING(12),
allowNull: false
},
phone: {
type: new DataTypes.STRING(12)
},
email: {
type: new DataTypes.STRING(50)
},
password: {
type: new DataTypes.STRING(100)
},
permissionId: {
type: new DataTypes.INTEGER,
references: {
model: 'Permission',
key: 'id'
}
}
}, {
sequelize: _instance,
tableName: 'User',
timestamps: false
});
User.belongsTo(Permission, { foreignKey: 'permissionId', as: 'Permission' });
User.belongsToMany(Project, { through: 'User_project' });
User.sync({ force: _reSync });
export default User;
项目类别:
import { Model, DataTypes } from 'sequelize';
import { DbInstance } from '../../main/context';
import * as Config from '../../config.json';
import User from './user';
var _reSync = Config.Database.ForceSync;
var _instance = new DbInstance().getInstance();
class Project extends Model {
id!: number;
status: string;
name!: string;
}
Project.init({
id: {
type: new DataTypes.INTEGER,
autoIncrement: true,
primaryKey: true
},
status: {
type: new DataTypes.CHAR(2)
},
name: {
type: new DataTypes.STRING(30),
allowNull: false
}
}, {
sequelize: _instance,
tableName: 'Project',
timestamps: false
});
Project.belongsToMany(User, { through: 'User_project' });
Project.sync({ force: _reSync });
export default Project;
我希望 sequelize 自动创建 N:N 'User_Project' 关系表。以本文档为例https://sequelize.org/v5/manual/associations.html
您应该在定义所有模型之后定义所有模型的所有关联。例如,在单独的 js 文件中或在每个模型中定义一个函数“关联”,然后在注册所有模型后调用这些函数。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句