Error when add new record with relationship db

Dear Forest Admin Support,

I have 3 table below:
table ‘department’: key is department_id
table ‘employee’: key is employee_id
table ‘department_employee’: foreign key fk_department to reference to ‘department’ table and foreign key fk_employee to reference to ‘employee’ table

but when i create new record to ‘department_employee’ or view list data of ‘department_employee’ table
always throws exception below:

2021-01-04T10:28:15.231395+00:00 app[web.1]: e[31m[forest] :deciduous_tree::deciduous_tree::deciduous_tree: Unexpected error: key.includes is not a functione[39m
2021-01-04T10:28:15.231403+00:00 app[web.1]: e[31mTypeError: key.includes is not a functione[39m
2021-01-04T10:28:15.231404+00:00 app[web.1]: e[31m at self.set (/app/node_modules/sequelize/lib/model.js:3669:19)e[39m
2021-01-04T10:28:15.231405+00:00 app[web.1]: e[31m at self._initValues (/app/node_modules/sequelize/lib/model.js:173:10)e[39m
2021-01-04T10:28:15.231405+00:00 app[web.1]: e[31m at new Model (/app/node_modules/sequelize/lib/model.js:118:10)e[39m
2021-01-04T10:28:15.231405+00:00 app[web.1]: e[31m at new cdt (/app/node_modules/sequelize/lib/sequelize.js:422:19)e[39m
2021-01-04T10:28:15.231406+00:00 app[web.1]: e[31m at new cdt (/app/node_modules/sequelize/lib/model.js:1524:18)e[39m
2021-01-04T10:28:15.231406+00:00 app[web.1]: e[31m at Function.build (/app/node_modules/sequelize/lib/model.js:2164:12)e[39m
2021-01-04T10:28:15.231407+00:00 app[web.1]: e[31m at model._setInclude (/app/node_modules/sequelize/lib/model.js:3797:85)e[39m

pls helpme review it.

Thanks & Regards!
Hung Chu

Hi @chumanhhung235,

Could you please share the model definition associated to the models you are refering to?
Could you also share with me your project name so I can check on my end if I can spot any issue ?

Thanks in advance :pray:

@jeffladiray: my project is “osscar”, model name related is CDT, Emplyee & CDT - Employee, pls help me review it.

Thanks & Regards!
Hung Chu

Hello @chumanhhung235,

I tried to reproduce your issue, but had no success.
Based from this database definition:

create table if not exists department
(
	id serial not null constraint department_pk primary key,
	name varchar
);

create table if not exists employee
(
	id serial not null constraint employee_pk primary key,
	name varchar
);

create table if not exists department_employee
(
	id serial not null constraint department_employee_pk primary key,
	department_id integer not null constraint department_employee_department_id_fk references department,
	employee_id integer not null constraint department_employee_employee_id_fk references employee,
	name varchar
);

Lumber will generate these models:

const Department = sequelize.define('department', {
    name: {
      type: DataTypes.STRING,
    },
  }, {
    tableName: 'department',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

const Employee = sequelize.define('employee', {
    name: {
      type: DataTypes.STRING,
    },
  }, {
    tableName: 'employee',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

const DepartmentEmployee = sequelize.define('departmentEmployee', {
    name: {
      type: DataTypes.STRING,
    },
  }, {
    tableName: 'department_employee',
    timestamps: false,
    schema: process.env.DATABASE_SCHEMA,
  });

Associations are:

Department.associate = (models) => {
    Department.hasMany(models.departmentEmployee, {
      foreignKey: {
        name: 'departmentIdKey',
        field: 'department_id',
      },
      as: 'departmentEmployees',
    });
  };

Employee.associate = (models) => {
    Employee.hasMany(models.departmentEmployee, {
      foreignKey: {
        name: 'employeeIdKey',
        field: 'employee_id',
      },
      as: 'departmentEmployees',
    });
  };

DepartmentEmployee.associate = (models) => {
    DepartmentEmployee.belongsTo(models.department, {
      foreignKey: {
        name: 'departmentIdKey',
        field: 'department_id',
      },
      as: 'department',
    });
    DepartmentEmployee.belongsTo(models.employee, {
      foreignKey: {
        name: 'employeeIdKey',
        field: 'employee_id',
      },
      as: 'employee',
    });
  };

And I have no issue adding new “DepartmentEmployee” records or list them.

Do you have an equivalent of these definitions?

Let me know :slight_smile: