Sequelize: throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model

纵饮孤独 提交于 2021-02-05 08:28:31

问题


In my program:

movie.js

import  { DataTypes } from 'sequelize'
import Actor from './actor'
import ActorMovies from './actormovies'
import { sequelize } from '../../db/seq'

const Movie = sequelize.define('Movie', { name: DataTypes.STRING });
Movie.belongsToMany(Actor, { through: ActorMovies });

export default Movie

actor.js

import { DataTypes } from 'sequelize'
import Movie from './movie'
import ActorMovies from './actormovies'
import { sequelize } from '../../db/seq'

const Actor = sequelize.define('Actor', { name: DataTypes.STRING });
Actor.belongsToMany(Movie, { through: ActorMovies });

export default Actor

actormovies.js

import { DataTypes } from 'sequelize'
import Movie from './movie'
import Actor from './actor'
import { sequelize } from '../../db/seq'

const ActorMovies = sequelize.define('ActorMovies', {
    MovieId: {
      type: DataTypes.INTEGER,
      references: {
        model: Movie, 
        key: 'id'
      }
    },
    ActorId: {
      type: DataTypes.INTEGER,
      references: {
        model: Actor, 
        key: 'id'
      }
    }
});

export default ActorMovies

It will threw an error

throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model

I guess it may be use diffierent sequelize in movie.js and actor.js, but I am not sure.

Does someone have already see an error that look like that ? I search for few days without any suitable issue, if someone could help I'll really appreciate,

thank !


回答1:


That's because you are trying to use .belongsToMany in their own files.

Simple explanation: When you require one of the model files, one of them is not defined as a sequelize model yet.

Try to use index.js file for your model folder. Take a look at my answer there

You can read @Dorian Jakov Stern Vukotic answer.



来源:https://stackoverflow.com/questions/62897998/sequelize-throw-new-errorthis-name-belongstomany-called-with-something-tha

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!