最近在用MySQL結合typeorm開發,其中出現一個錯誤,如下:
[Nest] 6424 - 2018-12-13 11:45:30 [ExceptionsHandler] ER_BAD_FIELD_ERROR: Unknown column 's.userId' in 'where clause' +38211ms
代碼如下:
await this.em.createQueryBuilder()
.update(SignDao)
.set({number: signDao.number, lastModifyTime: signDao.lastModifyTime, signCount: signDao.signCount})
.where('s.userId = :userId', {userId})
.execute();
查typeorm demo文檔發現,更新操作的寫法是:
import {getConnection} from "typeorm";
await getConnection()
.createQueryBuilder()
.update(User)
.set({ firstName: "Timber", lastName: "Saw" })
.where("id = :id", { id: 1 })
.execute();
對比發現是以爲 s 根本就不存在,沒定義,所以沒找到,去掉s 問題解決