grant macdonald
grant macdonald

Reputation: 25

Sequelize Node.js Model not associated to model

I have two models in my Node JS application, Users and Companies. When I try to use the include function when getting the companies, I get the error:

message: "users is not associated to companies!"

This is the user model

import { Sequelize, DataTypes } from 'sequelize';
import { Application } from '../declarations';

export default function (app: Application) {
  const sequelizeClient: Sequelize = app.get('sequelizeClient');
  const users = sequelizeClient.define('users', {
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      unique: true
    },
    password: {
      type: DataTypes.STRING,
      allowNull: true
    },
    role: {
      type: DataTypes.STRING,
      allowNull: true,
    },
    language: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: 'en',
    },
    firstName: {
      type: DataTypes.STRING,
      allowNull: false
    },
    lastName: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
    underscored: true,
    hooks: {
      beforeCount(options: any) {
        options.raw = true;
      }
    }
  });

  // eslint-disable-next-line no-unused-vars
  (users as any).associate = function (models: any) {
    (users as any).belongsTo(models.companies, {
      foreignKey: 'companyId',
      allowNull: true,
      onDelete: "CASCADE",
    });
  };

  return users;
}

and the companies model:

import { Sequelize, DataTypes, Model } from 'sequelize';
import { Application } from '../declarations';

export default function (app: Application) {
  const sequelizeClient: Sequelize = app.get('sequelizeClient');
  const companies = sequelizeClient.define('companies', {
    name: {
      type: DataTypes.STRING,
      allowNull: false
    },
    phone: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: '',
    },
    email: {
      type: DataTypes.STRING,
      allowNull: false,
      defaultValue: '',
    },
    active: {
      type: DataTypes.BOOLEAN,
      allowNull: false,
      defaultValue: true,
    },
  }, {
    underscored: true,
    hooks: {
      beforeCount(options: any) {
        options.raw = true;
      }
    }
  });

  // eslint-disable-next-line no-unused-vars
  (companies as any).associate = function (models: any) {
    (companies as any).hasMany(models.users);
    (companies as any).hasMany(models.patients);
  };

  return companies;
}

And the include function

function getSuperAdmin() {
  return (context: HookContext) => {
    const sequelizeClient = context.app.get("sequelizeClient");
      context.params.sequelize = {
        include: [
          {
            model: sequelizeClient.models.users,
            as: 'users',
            required: false,
            where: {
              '$users.role$': 'super_admin'
            }
          }
        ],
      };
      return context;
  }
}

When I include the companies model in the user query, it works correctly. But not the other way around. Any ideas?

Thanks

Upvotes: 0

Views: 379

Answers (0)

Related Questions