我们正在使用 Sails.js 作为后端框架将项目从 PHP 迁移到 Node.js。我们不能修改我们的数据库,并且必须为这个项目使用现有的数据库。
如果我migrate: "alter"
为新创建的模型保留 ,默认情况下,Sails 会将id
字段保留为整数。
但是,对于我们现有的数据库,id
字段大多是bigint
. 所以我定义migrate: "safe"
并继续创建模型。
现在我面临的问题是,当蓝图路由返回结果时,应该作为数字返回的 id 列值改为作为字符串返回。这是一个例子:
[
{
"starttime": "07:00:00",
"endtime": "14:00:00",
"id": "1"
},
{
"starttime": "14:00:00",
"endtime": "22:00:00",
"id": "2"
},
{
"starttime": "22:00:00",
"endtime": "07:00:00",
"id": "3"
}
]
我该如何解决这个问题?
这是我的模型:
module.exports = {
tableName: "timeslots",
autoCreatedAt: false,
autoUpdatedAt: false,
attributes: {
starttime: { type: "string", required: true },
endtime: { type: "string", required: true }
}
};
这是postgresql表定义
Table "public.timeslots"
Column | Type | Modifiers | Storage | Stats target | Description
-----------+--------+--------------------------------------------------------+----------+--------------+-------------
id | bigint | not null default nextval('timeslots_id_seq'::regclass) | plain | |
starttime | text | not null | extended | |
endtime | text | not null | extended | |
Indexes:
"idx_43504_primary" PRIMARY KEY, btree (id)
Referenced by:
TABLE "doctortimeslot" CONSTRAINT "doctortimeslot_ibfk_2" FOREIGN KEY (timeslot_id) REFERENCES timeslots(id) ON UPDATE CASCADE ON DELETE CASCADE