'How to create relationship between two tables using feathersjs, sequelize and postgresql

I have two models users.model.js and swing-trades.model.js when I am creating a relationship between these two models and running feathersjs using npm run dev`, I am getting an error.

throw new Error(${this.name}.hasMany called with something that's not a subclass of Sequelize.Model); ^

Error: users.hasMany called with something that's not a subclass of Sequelize.Mode

**users.model.js**

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const users = sequelizeClient.define('users', {

    email: {
      type: DataTypes.STRING,
      allowNull: true,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: true
    },
    username: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    firstName: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    lastName: {
      type: DataTypes.STRING,
      allowNull: true,
    }
  },
  {
    timestamps: true, // Enable timestamps
    createdAt: true, // Don't create createdAt
    updatedAt: true, // Don't create updatedAt
    // eslint-disable-next-line no-dupe-keys
    updatedAt: 'updateTimestamp', // updatedAt should be called updateTimestamp
  },
  {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  // eslint-disable-next-line no-unused-vars
  users.associate = function (models) {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    const {swingTrades} = models;

    users.hasMany(swingTrades, {as: 'swingTrades', foreignKey: 'userId'}); // will add userid to swingtrades model
  };

  return users;
};

swing-trades.model.js

// See http://docs.sequelizejs.com/en/latest/docs/models-definition/
// for more of what you can do here.
const Sequelize = require('sequelize');
const DataTypes = Sequelize.DataTypes;

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');
  const swingTrades = sequelizeClient.define('swing_trades', {
    company: {
      type: DataTypes.STRING,
      allowNull: true
    }
  },
  {
    timestamps: true, // Enable timestamps
    createdAt: false, // Don't create createdAt
    updatedAt: false, // Don't create updatedAt
    // eslint-disable-next-line no-dupe-keys
    updatedAt: 'updateTimestamp', // updatedAt should be called updateTimestamp
  },
  {
    hooks: {
      beforeCount(options) {
        options.raw = true;
      }
    }
  });

  // eslint-disable-next-line no-unused-vars
  swingTrades.associate = function (models) {
    // Define associations here
    // See http://docs.sequelizejs.com/en/latest/docs/associations/
    const {users} = models;

    swingTrades.belongsTo(users,{foreignKey: 'userId'});
    
  };

  return swingTrades;
};

I have not found any solutions for this.



Solution 1:[1]

That's because the model name and service name don't match. Here you have swingTrades

 **users.model.js**
 // eslint-disable-next-line no-unused-vars
 users.associate = function ({swingTrades) {
    users.hasMany(swingTrades, {
    as: 'swingTrades',
    foreignKey: 'userId' 
    }); 
 };

But in swing-trades.model.js you have swing_trades

...
const swingTrades = sequelizeClient.define('swing_trades', {
    company: {
      type: DataTypes.STRING,
      allowNull: true
    }
  },
...

Solution is to avoid spaces and - when creating new services. Use one word to name a service.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Youzef