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

查看:100
本文介绍了Sequelize: throw new Error(`${this.name}.belongsToMany 用不是 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆