【问题标题】:Sequelize: throw new Error(`${this.name}.belongsToMany called with something that's not a subclass of Sequelize.ModelSequelize: throw new Error(`${this.name}.belongsToMany 调用的东西不是 Sequelize.Model 的子类
【发布时间】:2020-07-22 13:04:57
【问题描述】:

在我的程序中:

电影.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

会报错

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

我猜它可能在movie.js和actor.js中使用了不同的sequelize,但我不确定。

是否有人已经看到类似的错误?我搜索了几天没有任何合适的问题,如果有人可以帮助我将不胜感激,

谢谢!

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    那是因为你试图在他们自己的文件中使用 .belongsToMany。

    简单解释:当您需要其中一个模型文件时,其中一个尚未定义为续集模型。

    尝试为您的模型文件夹使用 index.js 文件。 Take a look at my answer there

    您可以阅读@Dorian Jakov Stern Vukotic answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-04-20
      • 2020-09-26
      • 2019-05-21
      • 1970-01-01
      • 1970-01-01
      • 2022-01-04
      • 2020-09-21
      • 1970-01-01
      相关资源
      最近更新 更多