- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
Note
与 Subject
具有多对多关系
查询它的最佳方式是什么?我喜欢编写以下内容来获取给定注释上的所有主题:
const subjectRepo = connection.getRepository(Subject);
const response = await subjectRepo.find({
relations: ['notes'],
where: { note }
});
但这会返回所有主题,而不仅仅是笔记上的主题。
Reln 定义为:
@ManyToMany(() => Subject, (subject: Subject) => subject.notes)
subjects: Subject[];
-- 和 --
@ManyToMany(() => Note, note => note.subjects)
@JoinTable()
notes: Note[];
执行的查询是:
SELECT "Subject"."id" AS "Subject_id", "Subject"."name" AS "Subject_name", "Subject"."description" AS "Subject_description", "Subject"."createdDate" AS "Subject_createdDate", "Subject"."updatedDate" AS "Subject_updatedDate", "Subject"."notebookId" AS "Subject_notebookId", "Subject"."measurementsId" AS "Subject_measurementsId", "Subject_notes"."id" AS "Subject_notes_id", "Subject_notes"."content" AS "Subject_notes_content", "Subject_notes"."notedAt" AS "Subject_notes_notedAt", "Subject_notes"."createdDate" AS "Subject_notes_createdDate", "Subject_notes"."updatedDate" AS "Subject_notes_updatedDate", "Subject_notes"."notebookId" AS "Subject_notes_notebookId" FROM "subject" "Subject" LEFT JOIN "subject_notes_note" "Subject_Subject_notes" ON "Subject_Subject_notes"."subjectId"="Subject"."id" LEFT JOIN "note" "Subject_notes" ON "Subject_notes"."id"="Subject_Subject_notes"."noteId"
<小时/>
注意:您可以这样做:
return subjectRepo
.createQueryBuilder('subject')
.leftJoin('subject.notes', 'note')
.where('note.id = :id', { id: note.id })
.getMany();
但我希望有一种更少字符串和显式连接的方法
最佳答案
您尝试让 TypeORM 生成的 SQL 大致如下
SELECT *
FROM subject
JOIN subject_note AS jt on jt.subject_id = subject.id
WHERE jt.note_id = :id
repo.find
是不可能的在撰写本文时,无法创建 where
使用 repo.find(...)
连接表上的子句。您可以join
(doc)但是where
子句仅影响存储库的实体。
TypeORM
也会默默地忽略无效的 where 子句,所以要小心这些。
如果你想要所有subject
给定的note
,您要么需要使用查询生成器,就像您指出的那样,要么需要重新选择注释对象及其关系。
note = await noteRepo.find({
relations: ['subjects'],
where: { id: note.id }
});
const subjects = note.subjects
TypeORM
惰性关系如果想避免重新选择,需要使用 TypeORM
Lazy relations但这会迫使您将两个实体中的类型更改为 Promise
// note entity
@ManyToMany(() => Subject, (subject: Subject) => subject.notes)
subjects: Promise<Subject[]>;
// subject entity
@ManyToMany(() => Note, note => note.subjects)
@JoinTable()
notes: Promise<Note[]>;
有了这种惰性关系,您将需要await
在每次使用之前加载链接的注释,但您不需要提供与 find
的关系数组方法。
const note = await noteRepo.find({
where: { id: someId }
});
const subjects = await note.subjects
关于typeorm - 如何使用 TypeORM 查询多对多关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52246722/
我知道 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
我是一名优秀的程序员,十分优秀!