Javascript TypeForm可以找到具有关系的()
我第一次使用TypeORM,不幸的是不得不使用JavaScript。文档主要集中在TypeScript上,所以我在尝试获取一个包含所有相关其他条目的条目时遇到了困难Javascript TypeForm可以找到具有关系的(),javascript,node.js,postgresql,orm,typeorm,Javascript,Node.js,Postgresql,Orm,Typeorm,我第一次使用TypeORM,不幸的是不得不使用JavaScript。文档主要集中在TypeScript上,所以我在尝试获取一个包含所有相关其他条目的条目时遇到了困难 module.exports = { name: "recordings", columns: { id: { type: "uuid", primary: true, generated: "uuid" },
module.exports = {
name: "recordings",
columns: {
id: {
type: "uuid",
primary: true,
generated: "uuid"
},
title: {
type: "text",
unique: true,
nullable: false
},
file: {
type: "text",
unique: true,
nullable: false
}
},
relations: {
recordingAnnotations: {
target: "recordingAnnotations",
type: "one-to-many",
joinTable: true,
}
}
};
module.exports = {
name: "recordingAnnotations",
columns: {
id: {
type: "uuid",
primary: true,
generated: "uuid"
},
startsAt: {
type: "int",
nullable: false
},
endsAt: {
type: "int",
nullable: false
},
content: {
type: "text",
unique: true,
nullable: false
}
},
relations: {
recording: {
target: "recordings",
type: "many-to-one",
nullable: false,
onDelete: "CASCADE"
}
}
};
现在我想找到一个包含所有注释的录制条目
const repo = conn.getRepository(Recording.name);
const result = await repo.find({
where: { id },
relations: [ "recordingAnnotations" ]
});
我试图得到的结果如下所示:
id: "some-uuid",
title: "Recording 1",
file: "some-uuid",
annotations: [
{ id: "some-uuid", startsAt: 0, endsAt: 10, content: "Hello!" },
{ id: "some-uuid", startsAt: 20, endsAt: 25, content: "Bye!" },
]
我得到的错误是:
TypeError:无法读取未定义的属性'joinColumns'
在录制模式中,不需要joinTable,因为它只适用于多对多关系
relations: {
recordingAnnotations: {
target: "recordingAnnotations",
type: "one-to-many",
inverseSide: 'recording'
}
}
我们需要将inverseSide指定为recording
,因为我们在recordingAnnotations
模式中添加了recording
关系,并且许多人将外键放在当前实体表中
在recordingAnnotations模式中
relations: {
recording: {
target: "recordings",
type: "many-to-one",
nullable: false,
onDelete: "CASCADE",
joinColumn: true
}
}
我们需要指定joinColumn,它将在recordingAnnotations
表中添加recordingId
(外键)
另外,下面是使用一对多关系的完整示例: