thinkjs中修改默認主鍵
阿新 • • 發佈:2018-05-22
控制 sql stat 信息 自增 報錯 order alt 代碼
報錯信息:
{ Error: ER_BAD_FIELD_ERROR: Unknown column ‘a_role.id‘ in ‘field list‘
還原場景:
a_role這張表沒有自增的id,主鍵為roleid.如果在thinkjs的控制器中寫查詢這張表的數據sql時,就出現了這樣錯誤。
解決方法:
修改後的代碼:
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 returnthis.json({ total: result.count, rows: result.data }); 21 }
thinkjs中修改默認主鍵