- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我尝试通过 QueryBuilder 获取关系表,它工作正常,直到我尝试使用 skip/offset 和 take/limit。我期待这样的返回:
[
{
"id": 1, // order.id
"locations": [ ... ] array of locations with same order.id
},
{
"id": 2,
"locations": [ ... ]
},
{
"id": 3,
"locations": [ ... ]
}
]
order.entity.ts
@PrimaryGeneratedColumn({ name: 'id' })
public id!: number;
@OneToMany((type) => Location, (location) => location.order, {
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION',
})
public locations: Location[];
locations.entity.ts
@PrimaryGeneratedColumn({ name: 'id' })
public id!: number;
@ManyToOne((type) => Order, (order) => order.locations, {
nullable: false,
onDelete: 'NO ACTION',
onUpdate: 'NO ACTION',
})
@JoinColumn({ name: 'order_id' })
public order: Order = null;
[查询 A] 我通过以下代码获得了所需的输出:(但不使用 skip/take,输出位于该问题的顶部)
const orders = getRepository(Order)
.createQueryBuilder('order')
.where('order.customer_id = :customer', { customer: user.id })
.leftJoinAndSelect('order.locations', 'location', 'location.order_id = order.order_id')
.getMany(); // output count is 35, output count with limit/take of 10 would be 10
[查询 B] 如果我添加 skip/offset 和 take/limit,它将如下所示:
const orders = getRepository(Order)
.createQueryBuilder('order')
.where('order.customer_id = :customer', { customer: user.id })
.leftJoinAndSelect('order.locations', 'location', 'location.order_id = order.order_id')
.skip(0)
.limit(10)
.getMany(); // output count is 5
但是在这里,输出是正确的,但是长度/计数是完全错误的。 查询 A 找到 35 个订单,总有 2 个位置。如果我从 Query A 中删除 leftJoinAndSelect 并添加 skip and take,那么它也会找到 35 个订单。但是 查询 B,限制/获取为 10,给我的输出计数为 5。它减半了输出!如果 limit/take 等于 8,则输出长度为 4。输出减半!显然,getMany 有一些神奇之处,所以我找到了 getRawMany,它可以使输出加倍。因为对于每个订单,有 2 个位置。那也不是我需要的。而且这个输出的结构也是错误的(如下所示)。 getManyRaw 没问题,但如果我将它与 skip/take 一起使用则不行,因为输出显然是错误的,因为我需要每个订单的所有位置。 Group By 在这里没有帮助,因为这样我每个订单只有 1 个位置。
getRawMany 的输出是这样的
[
{
"order_id": 1,
"locations_id": 100
},
{
"order_id": 1,
"locations_id": 101
},
{
"id": 2,
"locations_id": 102
},
{
"id": 2,
"locations_id": 103
},
{
"id": 3,
"locations_id": 104
},
{
"id": 3,
"locations_id": 105
}
]
正如我所说,在这里使用 skip/take 会给我一个错误的结果。我怎样才能达到我的预期输出?
最佳答案
希望我能得到您的问题,基本上您想要实现的是跳过并接受父订单,而不管它有多少个子位置。这是正确的吗?
如果我的理解是正确的,你可以使用这样的方法:
const orders = getRepository(Order) // use await if it inside async function
.createQueryBuilder('order')
.addSelect('@row_number := ' +
'CASE WHEN @parent_id <> location.order_id ' +
'THEN @row_number + 1 ' +
'ELSE @row_number END AS rn, ' +
'@parent_id := location.order_id'
)
.where('order.customer_id = :customer', { customer: user.id })
.leftJoinAndSelect('order.locations', 'location', 'location.order_id = order.order_id')
.innerJoin('(SELECT @row_number := 0)', 'init_row', '1 = 1')
.innerJoin('(SELECT @parent_id := 0)', 'init_id', '1 = 1')
.orderBy('location.order_id')
.having('rn <= 10')
.getMany(); // now output should be 10 in case u want to skip use rn > 10 AND rn <= 20 condition
关于TypeORM - 使用带 leftJoinAndSelect 的 take/limit 没有按预期工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54338689/
我知道 typeorm 可以创建整个 db 模式,甚至可以将数据库模式与实体定义同步(请参阅 synchronize 中的 Connection Options ) 但在生产中,我想要求 typeor
我有一个 Role Entity 和一个 Route Entity,它们是树形结构,它们是 ManyToMany 关系。 现在我想通过 RoleRepository.find({relations:
我已经设置了 logging: true当createConnection()来自 TypeORM ,在大多数情况下都可以正常工作。 但是有一种情况,我的数据字段之一来自特定的 query/mutat
在 Rails 上,每个测试用例都会创建一个 ActiveRecord 事务,它允许测试所有内容,然后将数据库恢复到原始状态,而不必删除所有表或任何可能影响播种机等的内容。 可以在 Typeorm 上
Note 与 Subject 具有多对多关系 查询它的最佳方式是什么?我喜欢编写以下内容来获取给定注释上的所有主题: const subjectRepo = connection.getRepos
我使用 UUID 作为我的实体的主键,它工作得很好。但我想删除那些破折号。 现在 ID 被保存为 8e5365f4-3d42-4274-bafc-93b97bd6e3f2 36 个字符 我想要的是 8
我一整天都在尝试将实体保存到 MySQL 数据库中,遇到了巨大的困难。我正在使用 NestJS 和 TypeORM。 teacher.entity.ts import { BeforeInsert,
我也想在前端使用我用 TypeORM 创建的实体。有没有一种干净的方法可以做到这一点?或者我应该继续这样做。 此时,我的文件结构如下所示(前端的 Angular 8): /server /enti
我想得到一个 basic setup使用 TypeORM 工作,并在设置后收到此错误。 这是一个 REPL(只需执行 yarn install && yarn db:dev 后跟 yarn db:mi
我正在尝试通过 Nest/TypeORM 构建 SAAS 产品,我需要按子域配置/更改数据库连接。 customer1.domain.com => connect to customer1 datab
如何将 TypeORM 与 Better-sqlite3 一起使用? 上官方documentation ,有一个section形式更好-sqlite3。 我已经通过 typeorm@latest 安装
操作系统:macOS Sierra v 10.12.6 我正在尝试使用 typeorm 在 Typescript 中构建一个应用程序,这是我第一次使用其中任何一个。 我使用了以下两种方法来安装 typ
有没有一种简单的方法可以使用 DataSource 在 typeORM v.0.3.6 中播种数据? typeorm-seeding 似乎使用已弃用的 Connection。 最佳答案 找到这个包裹
我正在做一门类(class),我们使用 express-node postgres 和 react(使用 typescript)和 typeORM。到目前为止一切都运行良好,但我面临 typeORM
尽管进行了大量研究和反复试验,但不幸的是,我还没有针对以下用例的完美解决方案:一个用户有多个帖子,这被正确地实现为一对多关系。我想为用户加载他的所有帖子,但不是帖子的所有属性,只有某些属性。 方法 1
我用 NestJS 和 ORM TypeORM 做一个 API 我有实体: 用户 关系类型 UserRelationshipType(带有用户和关系类型 ForeignKey) 和关系(与 userR
我有两个实体 Tag 和 Photo: // tag.entity.ts import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm
我有以下要保存的实体: @Entity('approvals') export class Approval { @PrimaryGeneratedColumn() id: string;
我有命令: .leftJoinAndSelect("permission.user", "user") .where("permissi
我是使用 typeorm 的新手,这是我第二次与 typeorm 混淆,我有以下查询: SELECT t1.a,t1.b,t2.a (SELECT TOP 1 t1.a FROM table1
我是一名优秀的程序员,十分优秀!