【问题标题】:How to translate an SQL statement to TypeORM query builder?如何将 SQL 语句转换为 TypeORM 查询构建器?
【发布时间】:2018-01-17 08:28:47
【问题描述】:

如何将下面的代码转换为 TypeORM 查询构建器? 我正在尝试关注documentation

this.repository.manager.query(`
    SELECT item.name, item.id
    FROM item_location
    INNER JOIN item ON item.id = item_location.itemId
    WHERE item_location.locationId = ${queryObject.filter};
`)

谢谢。

【问题讨论】:

    标签: sql node.js database typescript typeorm


    【解决方案1】:

    我想通了。

    await getManager()
        .createQueryBuilder(Item, 'item')
        .select(['item.id', 'item.name'])
        .innerJoin('item.location', 'location')
        .where('location.id = :id', { id: queryObject.filter });
    

    【讨论】:

    • 什么是物品?我认为它是一个类,但仅此一项是行不通的。您可以更新 Item 类型定义吗?
    • 有没有在线工具可以轻松转换?
    【解决方案2】:

    如果您正在寻找以下内容,请告诉我。

    const item = await getManager().createQueryBuilder(Item, "item").innerJoinAndSelect("item.item_location", "item_location", "item_location.locationId = :queryfilter", { queryfilter: queryObject.filter}).select("item.name, item.id").getMany();
    


    【讨论】:

    • 该代码也引发了错误。未找到与实体中的属性路径 item_location 的关系。 item_location 是一个表,而不是一个属性。该表具有 locationId 和 itemId 属性
    猜你喜欢
    • 1970-01-01
    • 2021-10-02
    • 2021-07-29
    • 2023-01-20
    • 1970-01-01
    • 1970-01-01
    • 2019-10-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多