node.js - Nodejs 使用 Sequelize。“[].belongsTo 调用的不是 Sequelize.Model 在 Function 的子类。”
问题描述
[学到的知识]
我正在遵循一个基本教程来学习 sequelize 及其关联。
本书只展示hasMany
和belongsTo
举例。
凭借短暂的知识,我正在碰壁创建一些数据模式。
[我想做什么]
数据模式基本上是关于军事分支(或单位)的关联。
Atom 单元是一个团队。一个团队有一个直接的上级单位——一个部门。
或者,如果一个团队没有一个部门作为直接上级单位,那么,它的直接上级单位将是一个小队。
小队 > ( 部分 ) > 团队
我写了一些东西,但我得到了这个错误。
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
推荐阅读
- c# - 从 C# 类创建嵌套列表
- highcharts - 如何根据 xrange highcharts 中的 plotBand 选项自动调整条形?
- unity3d - unity新粒子系统转换
- python - 使用 Numpy 计算滚动回报/移动平均线
- node.js - 在 node-fetch 中 curl 对应的请求
- android - 使用 RecyclerView Android 加载列表中存在的所有数据
- php - 如何在 WooCommerce 中添加 Feed 广告
- c++ - 是否可以在编译时测试类型是否支持 C++ 中的负零?
- laravel - laravel 中的 hasMany 和 belongsTo
- html - html 不会转到 css,因此不会改变颜色