gpt4 book ai didi

node.js - Nodejs 使用 Sequelize。 "[].belongsTo called with something that' 不是 Sequelize.Model at Function 的子类。”

转载 作者:行者123 更新时间:2023-12-03 22:24:50 27 4
gpt4 key购买 nike

[学到的知识]
我正在学习 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)
这是我的代码。
模型/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;

模型/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'
});
};

}

模型/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'
});

};

}

模型/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){} 都没有代码错误。
但我不确定一个表是否可以有两个以上的 belongsTo
我很好奇我是否可以使用 belongsToMany 而不是 belongsTo
我该如何解决这个错误?
拜托,我需要帮助:(

最佳答案

您错过了 db.Squad = Squad; 中的 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);
这就是为什么在这条线上
db.Section.belongsTo(db.Squad, { 
foreignKey : "squadID",
targetKey : 'id'
});
你通过 undefined 作为 db.Squad

关于node.js - Nodejs 使用 Sequelize。 "[].belongsTo called with something that' 不是 Sequelize.Model at Function 的子类。”,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65276332/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com