{
"create_time": "2023-08-21T08:27:50.146Z",
"update_time": "2023-08-24T07:54:34.000Z",
"id": 1,
"name": "部门 1",
"code": "1111",
"down_dep": [
{
"create_time": "2023-08-21T08:27:50.146Z",
"update_time": "2023-08-24T07:55:25.000Z",
"id": 22,
"name": "部门 2",
"code": "100"
}
],
"up_dep": null
},
这种接口的数据怎么修改? 还有一个问题就是 DTO 中没有 create_time ,update_time 两个字段,修改时传入就会报错。
at Query.onResult (C:\pm-server\src\driver\mysql\MysqlQueryRunner.ts:222:33)
at Query.execute (C:\pm-server\node_modules\mysql2\lib\commands\command.js:36:14)
at PoolConnection.handlePacket (C:\pm-server\node_modules\mysql2\lib\connection.js:478:34)
at PacketParser.onPacket (C:\pm-server\node_modules\mysql2\lib\connection.js:97:12)
at PacketParser.executeStart (C:\pm-server\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\pm-server\node_modules\mysql2\lib\connection.js:104:25)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10) {
query: 'INSERT INTO `dept`(`create_time`, `update_time`, `id`, `name`, `code`, `upDepId`) VALUES (DEFAULT, DEFAULT, DEFAULT, ?, ?, DEFAULT)',
parameters: [ '456456', '1111' ],
driverError: Error: Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'
at Packet.asError (C:\pm-server\node_modules\mysql2\lib\packets\packet.js:728:17)
at Query.execute (C:\pm-server\node_modules\mysql2\lib\commands\command.js:29:26)
at PoolConnection.handlePacket (C:\pm-server\node_modules\mysql2\lib\connection.js:478:34)
at PacketParser.onPacket (C:\pm-server\node_modules\mysql2\lib\connection.js:97:12)
at PacketParser.executeStart (C:\pm-server\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.<anonymous> (C:\pm-server\node_modules\mysql2\lib\connection.js:104:25)
at Socket.emit (node:events:513:28)
at addChunk (node:internal/streams/readable:324:12)
at readableAddChunk (node:internal/streams/readable:297:9)
at Socket.Readable.push (node:internal/streams/readable:234:10) {
code: 'ER_DUP_ENTRY',
errno: 1062,
sqlState: '23000',
sqlMessage: "Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'",
sql: "INSERT INTO `dept`(`create_time`, `update_time`, `id`, `name`, `code`, `upDepId`) VALUES (DEFAULT, DEFAULT, DEFAULT, '456456', '1111', DEFAULT)"
},
code: 'ER_DUP_ENTRY',
errno: 1062,
sqlState: '23000',
sqlMessage: "Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'",
sql: "INSERT INTO `dept`(`create_time`, `update_time`, `id`, `name`, `code`, `upDepId`) VALUES (DEFAULT, DEFAULT, DEFAULT, '456456', '1111', DEFAULT)"
}```
值重复异常如何处理?自己写正则吗?
1
musi 2023-08-28 11:01:05 +08:00
俩问题都看不懂
1. 修改数据不是改数据库的吗,直接用 typeorm 提供的 API 不就可以改了? 2. 值重复不是数据库的限制吗?你到底想不想要这个限制,不想要去掉就好了 |
2
LandCruiser OP @musi 不行啊,update 不了,问题其实。第二个问题,我肯定是要这个字段的 unique 限制的,但是数据库返回给我的信息是一段 英文 Duplicate entry '456456' for key 'IDX_5eb4a4c9f25934f105299edffd'。 我现在想返回给用户的是{code:400 ,messgae:‘部门名称重复’}。这个过程需要我写什么代码呢? intercepetor ?还是什么
|
3
guiling 2023-08-28 13:41:37 +08:00 1
第一个问题我估计你配了关联关系吧,一般更新最好还是单表更新,这种级联的不太好控制,create_time ,update_time 可以考虑分别配置成 CURRENT_TIMESTAMP ,CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,数据库会自动为你更新
Duplicate entry xx for key 就是唯一索引重复,出数字段可以考虑直接用自增,一定要自己控制的话就封装个唯一 id 生成的工具类 |
4
guiling 2023-08-28 13:49:26 +08:00
如果是要捕获异常的话,唯一键的只能匹配关键词了,但一般出现这种情况就说明系统有问题了,因该考虑修复
|
5
lzgshsj 2023-08-28 15:13:18 +08:00 1
异常就用 exception filter 啊,@Catch('xxx'),xxx 就是 typeorm 的错误类
|
6
juzisang 2023-08-28 18:05:51 +08:00 1
DTO 校验一遍基本的类型错误,自己在写代码校验一遍重复值,手动抛错出去,exception filter 一般只是为了控制一些全局错误...
|
7
juzisang 2023-08-28 18:07:03 +08:00
别想着框架或者库帮你干这些,顶多帮你校验一些基本的错误,和业务相关的校验还是需要自己搞手动抛的...
|
8
encro 2023-08-29 09:15:49 +08:00 1
异常最简单的办法就是捕获,然后查询 message 包含 Duplicate entry ,就修改异常信息。
|
9
LandCruiser OP @juzisang 先查表,查有没有重复的,您是这个意思吗?先查再存,这种做法适用于生产吗?我服务端初学
|