nodejs sqlite报错 typeorm[ Expression tree is too large (maximum depth 1000)]

发布时间 2023-07-23 22:06:46作者: 随时静听

最近在给公司开发一个工具时,使用SQLite,然后突然发现报错:

(node:16195) UnhandledPromiseRejectionWarning: QueryFailedError: SQLITE_ERROR: Expression tree is too large (maximum depth 1000)
    at handler (/snapshot/server-work/node_modules/typeorm/driver/sqlite/SqliteQueryRunner.js:81:26)
    at Statement.errBack (/snapshot/server-work/node_modules/sqlite3/lib/sqlite3.js:15:21)
(Use `server-work --trace-warnings ...` to show where the warning was created)
(node:16195) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:16195) [DEP0018] Deprec

方法1

// 编译

tsc 
pkg -t win  package.json    

编译后运行程序时添加参数: --trace-warnings ,基本上可以解决报错,如果不能的话,请使用方法2

方法2

先看我的代码,查询当月的数据,然后执行remove进行删除查询到的数据,这个时候,typeorm最后会把你查的结果,使用id=xx or id=xxx 拼接,然后超过1000就会报错,解决方法就是使用IN,我这里就不直接查了后删除,直接根据条件直接删除

//先清空指定月的数据,然后进行添加
        // const currentMonthData= await this.workRepository.find({where:{month:month}})
        // await this.workRepository.remove(currentMonthData) //这里删除导致报错

由上面的代码修改成下面的代码

 await this.workRepository.createQueryBuilder().delete().where("month = :month ",{month:month})

你的代码如果有上面的报错,怎么办?

  • 开启SQL调试输出
  • 定位你SQL出问题的地方,一般是批量更新、批量删除的会经常出问题,重点关注这些,看是否是执行更新或删除时使用了结果作为参数

typeorm开启调试,指定logging级别即可

export const AppDataSource = new DataSource({
    type: "sqlite",
    database: config.sqliteConfig.dbname,
    synchronize: true,
    logging: ["query", "error"],
    entities: [User,Work],
    migrations: [User,Work],
    subscribers: [],
})