首页 > 解决方案 > 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;

标签: node.jsmodel-view-controllersequelize.js

解决方案


推荐阅读