thinkjs中修改默认主键

时间:2021-04-08 13:45:33

thinkjs中修改默认主键

报错信息:

{ Error: ER_BAD_FIELD_ERROR: Unknown column 'a_role.id' in 'field list'

还原场景:

a_role这张表没有自增的id,主键为roleid.如果在thinkjs的控制器中写查询这张表的数据sql时,就出现了这样错误。

解决方法:

thinkjs中修改默认主键

修改后的代码:

 1 async listAction() {
 2         let getData = this.get();
 3         let role = this.model('a_role');  4         role._pk = 'roleid';  5         let whereobj = {};//状态:1:正常 ;-1:禁用
 6         if (getData.roleid) {//角色ID
 7             whereobj['roleid'] =  getData.roleid;
 8         }
 9         if (getData.rolename) {//角色名称
10             whereobj['rolename'] = ['like', '%' + getData.rolename + '%'];
11         }
12         if (getData.state && getData.state != -99) {
13             whereobj.state = getData.state;
14         }
15         let result = await role.field('roleid,rolename,state')
16             .where(whereobj)
17             .order("roleid asc")
18             .page(getData.pageNumber, getData.pageSize)
19             .countSelect();
20         return this.json({ total: result.count, rows: result.data });
21     }