【问题标题】:How to add multiple foreign keys for one record in express js using sequelize如何使用sequelize在express js中为一条记录添加多个外键
【发布时间】:2021-07-21 23:52:19
【问题描述】:

我有一个名为Stories 的表,其中有几列和 3 个外键:类别、子类别和语言。

为了进行关联,我添加了如下的 sequelize 函数,将 CategoryIdSubCategoryIdLanguageId 列添加到 Story 表中。

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

如何将故事添加到故事表? 下面是我的代码。

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);
    });

虽然它正在向 Story 表添加故事,但它没有添加 categoryId、Sub categoryId 和 languageId,它只是为这些字段添加空值,如下面的屏幕截图所示。

我不知道如何将 CategoryId、SubCategoryId、LanguageId 添加到故事中。

【问题讨论】:

    标签: node.js express sequelize.js


    【解决方案1】:

    您在创建故事实例时使用的外键位于 camelCase 中,但您在 PascalCase 中定义了别名。

    更改关联定义中的任一别名

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

    或更改故事实例中的键

    常量故事 = 新故事({ 故事类型:故事类型, 标题:故事标题, 描述:描述, 类别:categoryId, SubCategoryId:subCategoryId, LanguageId:语言ID, 创建者:1 });

    注意:为所有关联添加外键约束,不允许在空值上插入/更新。

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

    【讨论】:

    • 你的两个解决方案都不适合我。我的代码正在向数据库添加记录,但它没有添加 categoryId、subcategoryId 和 languageId,当它被添加到表时它是空白的。您的第一个解决方案返回相同的结果,第二个解决方案属于最后一个 else 块 // 发送错误响应,因为所有承诺查询都没有完成任何类别、子类别或语言查询失败
    • 我添加了有问题的屏幕截图,你能看一下吗?
    • @PrasadGavande 您是否检查了导致问题的 3 个查询中的哪一个。
    • 它没有显示任何错误,我也得到了 ID,但是当我尝试在 Story.save() 中添加它时,它没有被添加。它是正确的添加方式吗?因为当我们和sequalize有关联时,如果是单表关联的话,会像Category.addStory()方法一样添加。
    • @PrasadGavande 我已经用问题陈述和解决方案更新了答案。
    猜你喜欢
    • 1970-01-01
    • 2019-06-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-09
    • 2021-08-26
    • 2017-06-15
    • 2019-07-26
    相关资源
    最近更新 更多