首页 > 解决方案 > 在 Console.log 和 api 响应中获取外键未定义

问题描述

我有report_data一个属于daily_entry表格的表格,但是当我调用api想要获取daily_entry表格的所有数据时,它会发送如下响应

输出

{
    "response_code": "0",
    "message": "Operation is successfully executed",
    "status": "success",
    "data": {
        "id": 1,
        "user_id": 1,
        "date": "12-10-2020",
         other data ....
        "is_active": true,
        "createdAt": "2020-10-21T06:25:57.877Z",
        "updatedAt": "2020-10-21T06:25:57.877Z",
        "report_datum": {
            "id": 1,
            "entry_i": 1, <<<<<<<----------OUTPUT
            "Date": null,
            "report_document_id": "2",
            "createdAt": "2020-10-21T06:26:02.642Z",
            "updatedAt": "2020-10-21T06:26:02.642Z"
        }
    },
    "level": "info",
    "timestamp": "2020-10-21T06:25:45.947Z"
}

预期的

{
    "response_code": "0",
    "message": "Operation is successfully executed",
    "status": "success",
    "data": {
        "id": 1,
        "user_id": 1,
        "date": "12-10-2020",
         other data ....
        "is_active": true,
        "createdAt": "2020-10-21T06:25:57.877Z",
        "updatedAt": "2020-10-21T06:25:57.877Z",
        "report_data": {
            "id": 1,
            "entry_id": 1,<<<<<-------------EXPECTED
            "Date": null,
            "report_document_id": "2",
            "createdAt": "2020-10-21T06:26:02.642Z",
            "updatedAt": "2020-10-21T06:26:02.642Z"
        }
    },
    "level": "info",
    "timestamp": "2020-10-21T06:25:45.947Z"
}

两个表之间的关系是hasOne

db.daily_entry.hasOne(db.report_data, { onDelete: "cascade", foreignKey: 'entry_id', foreignKeyConstraint: true, targetKey: 'id' });

我已经记录了来自 db 的数据,如下所示

 dataValues:{id: 1, entry_i: 1, daily_entry : 8468476, date: 23-10-2020, …}
 get entry_id:ƒ () {\n            return this.get(attribute);\n          }
 undefined

我已经检查了我的整个项目,没有像这样的名字entry_i

API I 代码如下

getdaily_entryById: async (req, res) => {
    sequelize.sequelize.transaction(async (t1) => {

        if (!req.params.id) {
            logger.warn(error.MANDATORY_FIELDS)
            return res.status(500).send(error.MANDATORY_FIELDS);
        }

        let data = await sequelize.daily_entry.findOne({
            where: { id: req.params.id },
            include: [
                sequelize.report_data
            ]
        });
        let result = error.OK
        result.data = data

        logger.info(result);
        return res.status(200).send(result);

    }).catch(function (err) {
        logger.warn(err)
        console.log(err)
        return res.status(500).send(error.SERVER_ERROR);
    });
}

daily_entry表模式

module.exports = function (sequelize, DataTypes) {

    const daily_entry = sequelize.define('daily_entry ', {
    user_id: {
        type: DataTypes.INTEGER(),
        allowNull: true
    },
    date: {
        type: DataTypes.STRING,
        allowNull: true
    },
    report_status: {
        type: DataTypes.STRING,
        allowNull: true
    },
    high_close: {
        type: DataTypes.DOUBLE(),
        allowNull: true
    },
    high_open: {
        type: DataTypes.DOUBLE(),
        allowNull: true
    },
    low_close: {
        type: DataTypes.DOUBLE(),
        allowNull: true
    },
    low_open: {
        type: DataTypes.DOUBLE(),
        allowNull: true
    },
    is_active: {
        type: DataTypes.BOOLEAN,
        allowNull: true
    }
 });

 return daily_entry 

};

report_data表架构

module.exports = function (sequelize, DataTypes) {

const report_data= sequelize.define('report_data', {
    daily_entry : {
        type: DataTypes.INTEGER(),
        allowNull: true
    },
    Date: {
        type: DataTypes.INTEGER(),
        allowNull: true
    },
    report_document_id: {
        type: DataTypes.TEXT,
        allowNull: true
    }
 },
    {
        tableName: 'report_data'
    });
 return report_data
};

那么,我在哪里做错了,为什么我会entry_i成为外键

标签: javascriptnode.jspostgresqlforeign-keyssequelize.js

解决方案


尝试将您的表名更改为CamelCase,然后尝试。

我知道这应该是评论,但目前我没有评论的特权


推荐阅读