使用 Sequelize 的 Node.js。 “[].belongsTo 在函数中使用不是 Sequelize.Model 的子类的东西调用。”

Nodejs using Sequelize. "[].belongsTo called with something that's not a subclass of Sequelize.Model at Function."

[学到的知识]

我正在按照基本教程学习 sequelize 及其关联。

本书仅显示 hasManybelongsTo 个示例。

由于知识匮乏,我在创建一些数据模式时碰壁了。

[我想做什么]

数据模式基本上是关于军事分支(或单位)协会的。

原子单位为一队。而一个团队有一个直属上级单位——科。

或者,如果一个队伍没有直属上级单位,那么,它的直属上级单位就是一个小队。

小队>(部分)>团队

我写了一些东西,但是我得到了这个错误。

Error: Section.belongsTo called with something that's not a subclass of Sequelize.Model
    at Function.<anonymous> (/Users/mac/Desktop/modeling/node_modules/sequelize/lib/associations/mixin.js:93:13)

这是我的代码。

model/index.js

const Sequelize = require('sequelize');

const Team = require("./team");
const Section = require("./section");
const Squad = require("./squad");

// const Platoon = require("./platoon");
// const Company = require("./company");
// const Battalion = require("./battalion");
// const Regiment = require("./regiment");
// const Brigade = require("./brigade");
// const Division = require("./division");
// const Corps = require("./corps");
// const Command = require("./command");
// const Unit = require("./branch");

// const DogTag = require("./dogtag");
// const Name = require("./name");
// const Rank = requrie("./rank");
// const Position = require("./position");
// const Branch = requrie("./branch")
// const Soldier = requrie("./soldier");



const env = process.env.NODE_ENV || 'development';
const config = require('../config/config')[env];
const db = {};

const sequelize = new Sequelize(config.database, config.username, config.password, config);
 
db.sequelize = sequelize;

db.Team = Team;
db.Section = Section;

Team.init(sequelize);
Section.init(sequelize);
Squad.init(sequelize);

Section.associate(db);
Squad.associate(db);
Team.associate(db);

module.exports = db;

model/team.js

const Sequelize = require("sequelize");

module.exports = class Team extends Sequelize.Model{
    static init(sequelize) {
        return super.init({
            name : {
                type : Sequelize.STRING(20), //STING == MySQL VARCHAR
                allowNull : false, // allowNull == MySQL NOT NULL
                unique : true, // unique == UNIQUE
            },
            created_at : {
                type : Sequelize.DATE, // DATE == MySQL DATETIME
                allowNull : false,
                defaultValue : Sequelize.NOW, // defaultValue == MySQL DEFAULT / Sequelize.NOW == now()
            },   // should you want ZEROFILL option, use [INTERGER.UNSIGNED].ZEROFILL
        },{
            sequelize, //connect with model/index.js
            timestamps : false,
            underscored : false, // createdAt => craeted_at
            modelName : 'Team',
            tableName : 'teams',
            paranoid : 'false', // if is true, deletedAt column will be craeted. 
            charset : 'utf8', 
            collate : 'utf8_general_ci',
        });
    }

    static associate(db) {
        db.Team.belongsTo(db.Section, { 
            foreignKey : "sectionID",
            targetKey : 'id' 
        });


        db.Team.belongsTo(db.Squad, { 
            foreignKey : "squadID",
            targetKey : 'id' 
        });
    };
    
}

model/section.js

const Sequelize = require("sequelize");

module.exports = class Section extends Sequelize.Model{
    static init(sequelize) {
        return super.init({
            name : {
                type : Sequelize.STRING(20), //STING == MySQL VARCHAR
                allowNull : false, // allowNull == MySQL NOT NULL
                unique : true, // unique == UNIQUE
            },
            created_at : {
                type : Sequelize.DATE, // DATE == MySQL DATETIME
                allowNull : false,
                defaultValue : Sequelize.NOW, // defaultValue == MySQL DEFAULT / Sequelize.NOW == now()
            },   // should you want ZEROFILL option, use [INTERGER.UNSIGNED].ZEROFILL
        },{
            sequelize, //connect with model/index.js
            timestamps : false,
            underscored : false, // createdAt => craeted_at
            modelName : 'Section',
            tableName : 'sections',
            paranoid : 'false', // if is true, deletedAt column will be craeted. 
            charset : 'utf8', 
            collate : 'utf8_general_ci',
        });
    }

    static associate(db) {
        db.Section.hasMany(db.Team, { 
            foreignKey : "sectionID",
            sourceKey : 'id' 
        });

        db.Section.belongsTo(db.Squad, { 
            foreignKey : "squadID",
            targetKey : 'id' 
        });

    };

}

model/squad.js

const Sequelize = require("sequelize");

module.exports = class Squad extends Sequelize.Model{
    static init(sequelize) {
        return super.init({
            name : {
                type : Sequelize.STRING(20), //STING == MySQL VARCHAR
                allowNull : false, // allowNull == MySQL NOT NULL
                unique : true, // unique == UNIQUE
            },
            created_at : {
                type : Sequelize.DATE, // DATE == MySQL DATETIME
                allowNull : false,
                defaultValue : Sequelize.NOW, // defaultValue == MySQL DEFAULT / Sequelize.NOW == now()
            },   // should you want ZEROFILL option, use [INTERGER.UNSIGNED].ZEROFILL
        },{
            sequelize, //connect with model/index.js
            timestamps : false,
            underscored : false, // createdAt => craeted_at
            modelName : 'Squad',
            tableName : 'squads',
            paranoid : 'false', // if is true, deletedAt column will be craeted. 
            charset : 'utf8', 
            collate : 'utf8_general_ci',
        });
    }

    static associate(db) {
        db.Squad.hasMany(db.Section, { 
            foreignKey : "squadID",
            sourceKey : 'id' 
        });


        db.Squad.hasMany(db.Team, { 
            foreignKey : "squadID",
            sourceKey : 'id' 
        });
    };
    
}

[问题]

虽然我知道的不多,但我很确定每一个static associate(db){}都没有代码错误。

但是我不确定一个table是否可以有两个以上belongsTo

而且我很好奇是否可以使用belongsToMany代替belongsTo

如何解决这个错误?

拜托,我需要帮助:(

您错过了 index.js 中的 db.Squad = Squad; 行:

db.Team = Team;
db.Section = Section;
db.Squad = Squad; // add this line

Team.init(sequelize);
Section.init(sequelize);
Squad.init(sequelize);

Section.associate(db);
Squad.associate(db);
Team.associate(db);

这就是为什么在这一行

db.Section.belongsTo(db.Squad, { 
            foreignKey : "squadID",
            targetKey : 'id' 
        });

你通过了 undefined 作为 db.Squad