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

Just Doo

[ learnt knowledge ]

I am fallowing a basic tutorial to learn sequelize and its associations.

And this book gives only hasMany and belongsTo examples.

With the short knowledge, I am hitting a wall to create some data schema.

[ What I am trying to ]

The data schema is basically about military branch(or unit) assoications.

Atom unit is a team. And a team has a direct superior unit - a section.

Or, if a team does not have a section as a direct superior unit, then, its direct superior unit will be a squad.

Squad > ( Section ) > Team

I wrote something, but I got this error.

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)

and Here is my code.

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 columm 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 columm 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 columm 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' 
        });
    };
    
}

[ Questions ]

Although I know only few things, I am pretty sure that each static associate(db){} has no code mistakes.

But I am not sure whether a table can have belongsTo more than two.

And I am very curious if I can use belongsToMany instead of belongsTo.

How can I solve this error?

Please, I need help :(

Anatoly

You missed db.Squad = Squad; line in index.js:

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);

That's why at this line

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

you passed undefined as db.Squad

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Sequelize for NodeJS and hasMany using UUID error

Using Sequelize with NodeJS and findByPk return pending

how to update Database using hooks/sequelize in nodejs

Nodejs Sequelize

insert a new record in nodejs using sequelize POST method

Nodejs Sequelize transaction using async/await not rolling back

Remove double quotes from a Table Name using SEQUELIZE Nodejs

How to apply Foreign Key in table model using Sequelize in NodeJs?

Updating a many to many join table using sequelize for nodejs

What is used in Nodejs - Sequelize for removing spaces when insert in DB using

Post start and stop time into mysql(sequelize) using NodeJS

Nodejs and async calls to mysql using sequelize block other users

How to store pdf file using nodeJS + multer + sequelize

Unable to create a many to many relationship using sequelize for mysql in nodejs

How to create new data with relation in nodejs using sequelize

Nodejs sequelize bulk upsert

Sequelize Migration NodeJS

Sequelize model loading in NodeJS

Sequelize: A is not associated to B" nodejs

NodeJS - Sequelize updating issue

Using Sequelize

I want to add data in four different table together using sequelize nodejs

how to create one to many relational record using nodejs and sequelize-typescript

Insert datas into mysql(Sequelize) using javascript(NodeJS) based on radio button value

How can users like each others post using sequelize postgres nodejs?

Nodejs Sequelize recursive async/await

sequelize findAll sort order in nodejs

How to create a TRIGGER in SEQUELIZE (nodeJS)?

Fixing the deprecated issue with Sequelize and nodejs