在Console.log和api响应中获取外键undefine



我有属于daily_entry表的表report_data,但当我像调用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' });

我已经记录了来自数据库的数据,就像下面一样

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

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

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

相关内容

  • 没有找到相关文章

最新更新