在 Console.log 和 api 响应中获取外键未定义

Getting foreign key undefine in Console.log and in api response

我有 table report_data 属于 daily_entry table 但是当我调用 api 时想要获取 [=20= 的所有数据] table 它发送如下响应

输出

{
    "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"
}

两者之间的关系 table 是 hasOne

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

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

 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我的代码在下面

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 table 架构

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 tables 架构

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 作为外键

尝试将您的 table 名称更改为 CamelCase 然后尝试。

我知道这应该是评论,但此时我没有评论的权限