定义模型sequelize时如何发现错误?

问题描述 投票:0回答:1

我第一次使用

sequelize
。我定义了两个模型,但运行后显示错误。我不知道如何解决这个问题。请帮帮我。
GalleryType
型号:

const GalleryType = sequelize.define("gallerytype",{
    _id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey:true
    },
    title:{
        type: DataTypes.STRING,
        allowNull: false,
        
        unique: {
            arg:true,
            name: 'Unique field',
            msg: "Gallery type must be unique"
        }
    }
})
GalleryType.hasMany(Gallery, {foreignKey: 'type', onDelete: 'CASCADE'})

Gallery
型号:

const Gallery = sequelize.define("gallery",{
    _id: {
        type: DataTypes.INTEGER,
        autoIncrement: true,
        allowNull: false,
        primaryKey:true
    },
    title:{
        type: DataTypes.STRING,
        allowNull: false
    },
    picture: DataTypes.STRING,
    url:{
        type: DataTypes.STRING,
        allowNull: false
    }
})
Gallery.belongsTo(GalleryType, { foreignKey: 'type', targetKey: '_id' });

Error: gallery.belongsTo called with something that's not a subclass of Sequelize.Model
node.js sequelize.js
1个回答
0
投票

您只是在模型之间存在循环依赖关系,您只需将所有关联定义移到模型模块之外,并将它们全部放在已注册所有模型的模块中,请参阅我的其他答案以了解整个想法

© www.soinside.com 2019 - 2024. All rights reserved.