0

I have some issue with "mst_rack is not associated to mst_part!"

RacksModel u can see my code below

import { Sequelize } from "sequelize";
import db from "../config/Database.js";
import Parts from "./PartModel.js";

const {DataTypes} = Sequelize;
const Racks = db.define('mst_rack',{
    id: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        primaryKey: true,
        autoIncrement: true,
        field: 'ID'
    },
    rack_no:{
        type: DataTypes.STRING(10)
    }, 
},{
  freezeTableName:true 
});

Racks.associate = function(models) {
    Parts.belongsTo(models.mst_rack, {foreignKey: 'id'});
}

export default Racks;

PartModel u can see my code below

import { Sequelize } from "sequelize";
import db from "../config/Database.js";
import Racks from "./RackModel.js";

const {DataTypes} = Sequelize;

const Parts = db.define('mst_part',{
    part_no:{
        allowNull: false,
        type: DataTypes.STRING(10)
    },
    part_name:{
        allowNull: false,
        type: DataTypes.STRING(100)
    },
    part_uniq:{
        allowNull: false,
        type: DataTypes.STRING(100)
    },
    mst_rackId: {
        type: DataTypes.INTEGER(11),
        allowNull: false,
        references: {
        model: 'mst_rack',
        key: 'ID'
    },
        field: 'id_rack'
    }
},{
      freezeTableName:true
});

Parts.associate = function(models) {
    Racks.hasOne(models.mst_rack, {foreignKey: 'id',sourceKey: 'mst_rackId'});

}

export default Parts;

Part Controller u can see my code below

import Parts from "../models/PartModel.js";
import Racks from "../models/RackModel.js"

export const getParts = async(req, res) => {
    try {
        const parts = await Parts.findAll({
            attributes:['id','part_no', 'part_name', 'part_uniq', 'id_rack'],
            include: [
                {model: Racks},
                {model: Racks, as: 'PartRack'},
            ]
    })
        res.json(parts);
    } catch (error) {
        console.log(error);
    }
 }

and i get some error below

Error Message

at mst_part._getIncludedAssociation (D:!Develop\be-stock-opname\node_modules\sequelize\lib\model.js:565:13) at mst_part._validateIncludedElement (D:!Develop\be-stock-opname\node_modules\sequelize\lib\model.js:502:53) at D:!Develop\be-stock-opname\node_modules\sequelize\lib\model.js:421:37 at Array.map () at mst_part._validateIncludedElements (D:!Develop\be-stock-opname\node_modules\sequelize\lib\model.js:417:39) at mst_part.findAll (D:!Develop\be-stock-opname\node_modules\sequelize\lib\model.js:1124:12) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async getParts (file:///D:/!Develop/be-stock-opname/controllers/Part.js:6:23)

And the simple question - what's wrong ? :) I want to get Part with specific Rack

1 Answers1

0

Remove models cross-references in their modules. Exchange association definitions the way that the main model should be the model from the current module (so you don't need to import the other model): RacksModel

Racks.associate = function(models) {
    Racks.hasOne(models.mst_part, {foreignKey: 'id',sourceKey: 'mst_rackId'});
}

PartModel

Parts.associate = function(models) {
   Parts.belongsTo(models.mst_rack, {foreignKey: 'id'});
}

Also I didn't see the association definition with the alias PartRack indicated in the query and I didn't found the code that calls associate methods of all models.

All in all I recommend to use the approach I showed in my other answer here to register and use models properly.

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • mst_rack is not associated to mst_part! Part Controller ` const parts = await Parts.findAll({ attributes:['id','part_no', 'part_name', 'part_uniq', 'id_rack'], include: [ {model: Racks} ] })` RackModel `Racks.associate = function(models) { Racks.hasOne(models.mst_part, {foreignKey: 'id',sourceKey: 'mst_rackId'}); }` Part Model `Parts.associate = function(models) { Parts.belongsTo(models.mst_rack, {foreignKey: 'id'}); }` – Firdan Mahendra Wardana Jun 25 '23 at 03:34
  • Check if you call `associate` method for all models after you registered them all. Check if associations prop in the `Parts` model is not empty right before calling `findAll` – Anatoly Jun 25 '23 at 17:56
  • And please update the post with the actual code and corrected association defintions – Anatoly Jun 25 '23 at 17:56