i started to use the models/index file that squeli-cli generate and now this is generateing me a error in the asociations
my model product my product can belog to many categories:
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Product extends Model {
static associate(models) {
Product.belongsTo(models.ProductType,{as:'family',foreignKey:'product_family'});
}
};
Product.init({
id:{
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate:{
isAlphanumeric: true
}
},
price:{
type: DataTypes.FLOAT,
allowNull: false,
validate:{
len: [0.1,10_000],
isEven(value) {
if (value <= 0.1) {
throw new Error('price must be 0.2$ or higher');
}
}
}
},
product_code:{
type: DataTypes.STRING,
unique:true,
allowNull: false,
validate:{
isAlphanumeric: true,
}
},
product_quantity:{
type: DataTypes.INTEGER,
allowNull: false,
validate:{
isNumeric: true,
len: [1,1_000_000_000],
isEven(value) {
if (value <= 0) {
throw new Error('quantity must be 1 or higher');
}
}
}
},
notification: {
type: DataTypes.BOOLEAN,
// allowNull defaults to true
},
}, {sequelize,modelName: 'Product',});
return Product;
};
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
my product types model this model havemany products:
'use strict';
const { Model } = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class ProductType extends Model {
static associate(models) {
ProductType.hasMany(models.Product, {as:'ProductType', foreignKey:'product_family'});
}
};
ProductType.init({
id:{
allowNull: false,
autoIncrement: true,
primaryKey: true,
type: DataTypes.INTEGER
},
name: {
type: DataTypes.STRING,
allowNull: false,
validate:{
isAlphanumeric: true
}
},
}, { sequelize,modelName: 'product_Type',});
return ProductType;
};
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
and this is my models/index
'use strict';
const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');
const basename = path.basename(__filename);
//config
const config = require('../config/database');
//declaracion de objeto db
const db = {};
//inicializar la conexion
const sequelize = new Sequelize(config.database,config.username,config.password,{
host: config.host,
dialect: config.dialect,
});
fs
.readdirSync(__dirname)
.filter(file => {
return (file.indexOf('.') !== 0) && (file !== basename) && (file.slice(-3) === '.js');
})
.forEach(file => {
const model = require(path.join(__dirname, file))(sequelize, Sequelize.DataTypes);
//cada modelo del directorio lo vinculamos a db
db[model.name] = model;
});
//realizar las asociaciones de los modelos
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db);
}
});
db.sequelize = sequelize;
db.Sequelize = Sequelize;
module.exports = db;
<iframe name="sif3" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
and y got this error error image
how can i solve this
CodePudding user response:
You named the model ProductType
as product_Type
but you're trying to access it by using models.ProductType
instead of models.product_Type
.