node.js - Error: CustomisedVideoAttributes.hasOne called with something that's not a subclass of Sequelize.Model
问题描述
I am having the error when trying to implement the association reltionship between customevideoId and youtubevideoId. I already tried various solutions. at this point am not sure of what am doing wrong
CustomisedVideoAttributes class
'use strict';
const { Model} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class CustomisedVideoAttributes extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate(models) {
// define association here
this.hasOne(models.youtubevideoattributes)
}
};
CustomisedVideoAttributes.init({
singleEvent: DataTypes.BOOLEAN,
viewsCount: DataTypes.INTEGER,
likesCount: DataTypes.INTEGER,
dislikeCount: DataTypes.INTEGER
}, {
sequelize,
modelName: 'CustomisedVideoAttributes',
});
return CustomisedVideoAttributes;
};
YoutubeVideoAttributes
'use strict';
const { Model} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class YoutubeVideoAttributes extends Model {
/**
* Helper method for defining associations.
* This method is not a part of Sequelize lifecycle.
* The `models/index` file will call this method automatically.
*/
static associate= (models)=> {
// define association here
this.belongsTo(models.customoisedVideoAttributes,{
foreignKey: 'customVideoId',
onDelete: 'CASCADE',
});
}
};
YoutubeVideoAttributes.init({
videoId: DataTypes.INTEGER,
youtubeId: DataTypes.INTEGER,
uploadDate: DataTypes.DATE,
eventDate: DataTypes.DATE
}, {
sequelize,
modelName: 'YoutubeVideoAttributes',
});
return YoutubeVideoAttributes;
};
Index.js To handle the call to the model. The index.js file was automatically generated when i ran the command sequelize.init. The models were also automatically created When I ran the sequelizer command to create sequelizer models. The only code i had to do was the content of the associate method and that's the code giving me errors.
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
const env = process.env.NODE_ENV || 'development';
const config = require(__dirname + '/../config/config.json')[env];
const db = {};
let sequelize;
if (config.use_env_variable) {
sequelize = new Sequelize(process.env[config.use_env_variable], config);
} else {
sequelize = new Sequelize(config.database, config.username, config.password, config);
}
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
db[model.name] = model;
});
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
解决方案
推荐阅读
- c# - 使用实体框架核心 2 进行排序、过滤和分页
- python - 如果函数返回某个字符串,则不要运行插入语句
- java - 在 ListView 上显示来自 EditText 的文本
- java - 从自己编写的网络服务器调用网站时没有显示图像如何解决
- javascript - 按文本字段过滤列表
- c# - 按列表排序
> 仅按项目 2 - reactjs - 为什么当我尝试从 cookie 访问会话 id 的值时我得到未定义
- r - 如何通过在 r 中使用带有数字的起始字母来选择和替换字符变量的多个值
- javascript - jQuery 可排序自己的元素作为占位符
- kotlin - 在android中使用recycleview