Prasad Gavande
Prasad Gavande

Reputation: 249

How to add multiple foreign keys for one record in express js using sequelize

I have a table called Stories there are few columns and 3 foreign keys: category, sub category and language.

To make association, I have added sequelize function as below which added CategoryId, SubCategoryId and LanguageId columns to the Story table.

story.belongsTo(category, { as: 'Category' });
story.belongsTo(subCategory, { as: 'SubCategory' });
story.belongsTo(language, { as: 'Language' });

How can I add story to story table? Below is my code.

const Category = require('../models/category');
const SubCategory = require('../models/subCategory');
const Language = require('../models/language');

exports.postStory = (req, res, next) => {
    const storyTitle = req.body.title;
    const description = req.body.description;    
    const categoryId = req.body.categoryId;
    const subCategoryId = req.body.subCategoryId;
    const languageId = req.body.languageId;


    Category.findOne({
        where: {
            id: categoryId
        }
    }).then(category => {
        return SubCategory.findOne({
            where: {
                id: subCategoryId
            }
        })
    }).then(subcategory => {
        return Language.findOne({
            where: {
                id: languageId
            }
        }).then(language => {
            //save operation here
            const story = new Story({
                story_type: storyType,
                title: storyTitle,
                description: description,
                categoryId: categoryId,
                subCategoryId: subCategoryId,
                languageId: languageId,
                createdBy: 1
            });
            return story.save()
                .then((result) => {
                    res
                        .status(201)
                        .json({
                            message: "Story added to database",
                            statusCode: 201,
                            CreatedBy: 1,
                            result: result,
                        });
                })
        })
    }).catch((error) => {
        if (!error.statusCode) {
            error.statusCode = 500;
        }
        next(error);
    });

Although it is adding stories to Story table but it is not adding categoryId, Sub categoryId and languageId, it is adding only null values for those fields as per below screen capture.

enter image description here

I cannot figure out how to add CategoryId, SubCategoryId, LanguageId to story.

Upvotes: 1

Views: 975

Answers (1)

Chandan
Chandan

Reputation: 11797

The foreign key you are using while creating your story instance is in camelCase but you have defined the alias in PascalCase.

Change either alias in you association definition

story.belongsTo(category, { as: 'category' });
story.belongsTo(subCategory, { as: 'subCategory' });
story.belongsTo(language, { as: 'language' });

Or Change key in story instance

const story = new Story({
  story_type: storyType,
  title: storyTitle,
  description: description,
  Category: categoryId,
  SubCategoryId: subCategoryId,
  LanguageId: languageId,
  createdBy: 1
});

Note: Add foreign key constraint for all associations to not allow insert/update on null value.

story.belongsTo(category, {
  as: "category",
  foreignKey: {
    allowNull: false
  }
});

Upvotes: 1

Related Questions