0
Good am using sequelize
along with nodejs
and I’m having difficulties with a relationship.
I have 3 tables where (location, Equipment, Shed, area) where in one location I have 1 equipment (Equipment), is in a shed (Shed) and belongs to an area.
The models are like this:
local:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('local', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
required: true,
unique: true
},
name: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idEquipment: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idShed: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
situation: {
type: dataTypes.BOOLEAN,
allowNull: false,
required: true,
defaultValue: true
},
capacity: {
type: dataTypes.FLOAT,
allowNull: false,
required: true
},
posX: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
posY: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
posZ: {
type: dataTypes.STRING,
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.belongsTo(models.area, {
foreignKey: 'idArea'
});
model.belongsTo(models.equipment, {
foreignKey: 'idEquipment'
});
// model.belongsTo(models.shed, {
// foreignKey: 'idShed'
// });
};
return model;
};[]
Equipment:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('equipment', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
description: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
idHangar: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
idControlPlan: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
dateControlPlan: {
type: dataTypes.DATE,
allowNull: true,
required: false
},
idUserControlPlan: {
type: dataTypes.STRING(200),
allowNull: true,
required: false
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.hasOne(models.local, {
foreignKey: 'idEquipment'
});
};
return model;
};
Shed:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('shed', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
idArea: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
description: {
type: dataTypes.STRING(200),
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
// model.hasOne(models.local, {
// foreignKey: 'idShed'
// });
model.belongsTo(models.area, {
foreignKey: 'idArea'
});
};
return model;
};
area:
import { Sequelize, DataTypes } from 'sequelize';
export default (sequelize, dataTypes) => {
const model = sequelize.define('area', {
id: {
type: dataTypes.STRING(200),
primaryKey: true,
allowNull: false,
unique: true,
required: true
},
description: {
type: dataTypes.STRING,
unique: true,
allowNull: false,
required: true
},
status: {
type: dataTypes.BOOLEAN,
allowNull: true,
defaultValue: true
}
}).schema('public');
model.associate = (models) => {
model.belongsToMany(models.company, {
through: 'companyArea',
foreignKeyConstraint: true,
foreignKey: 'idArea'
});
model.hasOne(models.shed, {
foreignKey: 'idArea'
});
model.hasOne(models.local, {
foreignKey: 'idArea'
});
};
return model;
};
When I add Shed’s relationship, it informs me that the relationship does not exist, when I take everything off goes normally:
[SQL Error] Sequelizedatabaseerror: relation "public.Sheds" does not exist EXIT
[SQL Error] relation "public.Sheds" does not exist EXIT
I’m using a database postgres
.
Where can be the mistake, would be a clerical error ? Or a model can not have a relationship belongsTo
and hasOne
at the same time ?
The error is running when you run the Migration or when you do a database query?
– Rafael Alves Costa
This does not answer the question. When you have reputation enough, you’ll be able to leave comments on any post but until then, write only answer that no depend on more information of who asked. - Of Revision
– RXSD