Neji Soltani
Neji Soltani

Reputation: 1511

Returning parent attributes via sequelize

I am working on a NodeJs application using sequelize as an ORM with exciting database , so I had to use sequelize model generator in order to generate the models for my application.
Here's an example of the generation output:

Category.js

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('category', {
        id: {
            type: DataTypes.STRING(128),
            allowNull: false,
            primaryKey: true,
            field: 'id'
        },
        name: {
            type: DataTypes.TEXT,
            allowNull: true,
            field: 'name'
        }
    }, {
        tableName: 'category'
    });
};

Product.js

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('product', {
        id: {
            type: DataTypes.STRING(128),
            allowNull: false,
            primaryKey: true,
            field: 'id'
        },
        category: {
            type: DataTypes.STRING(128),
            allowNull: false,
            references: {
                model: 'category',
                key: 'id'
            },
            field: 'category'
        },
        name: {
            type: DataTypes.TEXT,
            allowNull: true,
            field: 'name'
        }
    }, {
        tableName: 'product'
    });
};

and then inside my controller I have this query :

models.product.findOne({
    where: {
        id: req.body.id
    }
}).then(function (obj) {
    //return the product data
    console.log(product.category) //works
    console.log(product.category.name) //return undefined
});

The question is how can I access to the parent table attribute via the same query findOne ? Is there something like or equivalent to product.category.id ?

Upvotes: 0

Views: 1187

Answers (1)

Ashh
Ashh

Reputation: 46441

if you have associated both the models... then try this

module.exports = function(sequelize, DataTypes) {
    return sequelize.define('product', {
        id: {
            type: DataTypes.STRING(128),
            allowNull: false,
            primaryKey: true,
            field: 'id'
        },
        category_id: {
            type: DataTypes.STRING(128),
            allowNull: false,
            references: {
                model: 'category',
                key: 'id'
            },
            field: 'category'
        },
        name: {
            type: DataTypes.TEXT,
            allowNull: true,
            field: 'name'
        }
    }, {
        tableName: 'product'
    });
}

models.product.findOne({
    where: {
        id: req.body.id
    },
    include: [{
        model: category,
        required: false
      }]
}).then(function (obj) {
    //return the product data
    console.log(product.category) //works
    console.log(product.category.name) //return undefined
});

Associate like this

product.hasMany(db.category, {  
  foreignKey: 'category_id',
  onDelete: 'cascade'
});

category.belongsTo(db.product, {
  foreignKey: 'category_id',
  targetKey: 'id',
  constraints: true
});

Upvotes: 1

Related Questions