Sequelize: TypeError: User.hasMany is not a function

Viewed 7227

I am having this weird behavior. I have a User model, and a Client model. User has many Clients. I am using this docs: http://docs.sequelizejs.com/manual/tutorial/associations.html

When I run the server, Sequelize throws TypeError: User.hasMany is not a function

Node version: 8.9.1
Dialect: postgres
Database version: Postgres 10
Sequelize version: 4.22.15

The following files are all in the same folder

user.js model

const Sequelize = require('sequelize');
const db = require('../index.js'); //This is an instance of new Sequelize(...)

const tableName = 'users';

const User = db.define('user', {
  firstName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  lastName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  username: { //Username will be the email
    type: Sequelize.STRING(80),
    allowNull: false,
    unique: true,
    validate: {
      isEmail: true
    }
  },
  password: {
    type: Sequelize.STRING,
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  isAdmin: {
    type: Sequelize.BOOLEAN,
    allowNull: false,
    defaultValue: false
  },
  isActive: {
    type: Sequelize.BOOLEAN,
    allowNull: false,
    defaultValue: false
  }

}, { tableName });

module.exports = User;

client.js model

'use strict'

const Sequelize = require('sequelize');
const db = require('../index.js');

const instanceMethods = {
  toJSON() {
    const values = Object.assign({}, this.get());

    return values;
  },
};

const Client = db.define('clients', {
  ibid: {
    type: Sequelize.BIGINT,
    allowNull: true,
    defaultValue: null
  },
  firstName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  lastName: {
    type: Sequelize.STRING(50),
    allowNull: false,
    validate: {
      notEmpty: true
    }
  },
  agreementSigned: {
    type: Sequelize.BOOLEAN,
    allowNull: false,
    defaultValue: false
  },
  totalBalance: {
    type: Sequelize.DECIMAL(11,2),
    allowNull: true,
    defaultValue: null
  },
  currentAllocation: {
    type: Sequelize.STRING,
    allowNull: true,
    defaultValue: null
  }

}, { instanceMethods });

module.exports = Client;

index.js models

'use strict';

const User = require('./user')
const Client = require('./client');

User.hasMany(Client);
Client.belongsTo(User);

module.exports = {User, Client};
1 Answers
Related