Sequelize: throw new Error(`${this.name}.belongsToMany 调用的东西不是 Sequelize.Model 的子类 [英] Sequelize: throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model
问题描述
在我的程序中:
电影.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
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
会报错
throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.Model
我猜它可能在movie.js和actor.js中使用了不同的sequelize,但我不确定.
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,
谢谢!
推荐答案
那是因为你试图在自己的文件中使用 .belongsToMany.
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.
尝试为您的模型文件夹使用 index.js 文件.看看我的回答
Try to use index.js file for your model folder. Take a look at my answer there
你可以阅读@Dorian Jakov Stern Vukotic 回答.
You can read @Dorian Jakov Stern Vukotic answer.
这篇关于Sequelize: throw new Error(`${this.name}.belongsToMany 调用的东西不是 Sequelize.Model 的子类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!