sequelize / sequelize

Feature-rich ORM for modern Node.js and TypeScript, it supports PostgreSQL (with JSON and JSONB support), MySQL, MariaDB, SQLite, MS SQL Server, Snowflake, Oracle DB (v6), DB2 and DB2 for IBM i.
https://sequelize.org/
MIT License
29.25k stars 4.25k forks source link

users.belongsToMany called with something that's not a subclass of Sequelize.Model Solve This Error #17392

Open aritradapl opened 2 weeks ago

aritradapl commented 2 weeks ago

const { DataTypes } = require('sequelize'); const { sequelize } = require('../config/database'); const Role = require('../models/Role'); const UserRole = require('../models/UserRole'); const Permission = require('../models/Permission'); const UserPermission = require('../models/UserPermission');

const User = sequelize.define('users', { id: { type: DataTypes.INTEGER, autoIncrement: true, primaryKey: true }, name: { type: DataTypes.STRING, allowNull: false, }, lastName: { type: DataTypes.STRING, defaultValue: '' }, email: { type: DataTypes.STRING, unique: true, allowNull: false, }, emailToken: { type: DataTypes.TEXT }, emailTokenExpired: { type: DataTypes.DATE }, isEmailVerified: { type: DataTypes.TINYINT, defaultValue: 0, comment: '1 = Verified, 0 = Not Verified', allowNull: false, }, mobile: { type: DataTypes.STRING, unique: true, }, mobileToken: { type: DataTypes.TEXT }, mobileTokenExpired: { type: DataTypes.DATE }, isMobileVerified: { type: DataTypes.TINYINT, defaultValue: 0, comment: '1 = Verified, 0 = Not Verified', allowNull: false, }, password: { type: DataTypes.STRING, }, passwordToken: { type: DataTypes.TEXT }, passwordTokenExpired: { type: DataTypes.DATE }, otp: { type: DataTypes.STRING }, otpExpired: { type: DataTypes.DATE }, authToken: { type: DataTypes.TEXT }, googleId: { type: DataTypes.STRING }, facebookId: { type: DataTypes.STRING }, appleId: { type: DataTypes.STRING }, bankId: { type: DataTypes.STRING }, status: { type: DataTypes.ENUM('active', 'inactive', 'delete'), defaultValue: 'active', allowNull: false, }, });

User.belongsToMany(Role, { through: UserRole, targetKey: 'id', foreignKey: 'userId' }); User.belongsToMany(Permission, { through: UserPermission, targetKey: 'id', foreignKey: 'userId' });

sequelize.sync({ // alter: true }).then(() => { console.log('users table synchronized successfully!'); }).catch((error) => { console.error('Unable to synchronized table: ', error); });

module.exports = { User };

/media/dapl-asset-253/dbe82a71-4457-4c23-a401-c78b3cb4b732/var/www/html/Node Office Projects Aritra/crowdfunding-backend/node_modules/sequelize/lib/associations/mixin.js:33 throw new Error(${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model); ^

Error: users.belongsToMany called with something that's not a subclass of Sequelize.Model

How To Solve This Error ?