mirror of
https://github.com/eggjs/egg.git
synced 2024-12-04 07:14:30 +00:00
docs: example for custom id when mysql update (#2165)
This commit is contained in:
parent
10327e1850
commit
a86334c595
@ -233,6 +233,25 @@ const result = await this.app.mysql.update('posts', row); // update records in '
|
||||
|
||||
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE id = 123 ;
|
||||
|
||||
// check if update is success or failure
|
||||
const updateSuccess = result.affectedRows === 1;
|
||||
|
||||
// if primary key is your custom id,such as custom_id,you should config it in `where`
|
||||
const row = {
|
||||
name: 'fengmk2',
|
||||
otherField: 'other field value', // any other fields u want to update
|
||||
modifiedAt: this.app.mysql.literals.now, // `now()` on db server
|
||||
};
|
||||
|
||||
const options = {
|
||||
where: {
|
||||
custom_id: 456
|
||||
}
|
||||
};
|
||||
const result = await this.app.mysql.update('posts', row, options); // update records in 'posts'
|
||||
|
||||
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ;
|
||||
|
||||
// check if update is success or failure
|
||||
const updateSuccess = result.affectedRows === 1;
|
||||
```
|
||||
|
||||
@ -249,6 +249,25 @@ const result = await this.app.mysql.update('posts', row); // 更新 posts 表中
|
||||
|
||||
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE id = 123 ;
|
||||
|
||||
// 判断更新成功
|
||||
const updateSuccess = result.affectedRows === 1;
|
||||
|
||||
// 如果主键是自定义的 ID 名称,如 custom_id,则需要在 `where` 里面配置
|
||||
const row = {
|
||||
name: 'fengmk2',
|
||||
otherField: 'other field value', // any other fields u want to update
|
||||
modifiedAt: this.app.mysql.literals.now, // `now()` on db server
|
||||
};
|
||||
|
||||
const options = {
|
||||
where: {
|
||||
custom_id: 456
|
||||
}
|
||||
};
|
||||
const result = await this.app.mysql.update('posts', row, options); // 更新 posts 表中的记录
|
||||
|
||||
=> UPDATE `posts` SET `name` = 'fengmk2', `modifiedAt` = NOW() WHERE custom_id = 456 ;
|
||||
|
||||
// 判断更新成功
|
||||
const updateSuccess = result.affectedRows === 1;
|
||||
```
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user