我在nodejs中有2个实体,在“一对多”关系中键入ORM。第三个关系表包含两个实体之间的连接。
@Entity({
name: 'meeting_entity',
})
export class MeetingEntity {
@PrimaryColumn()
id: string;
@OneToMany(type => CaseExtensionEntity, caseExtensionEntity => caseExtensionEntity.id, {cascade: true})
@JoinTable({
name: 'meeting_entity_cases',
joinColumns: [
{
name: 'meeting_entity_id'
}
],
inverseJoinColumns: [
{
name: 'cases_id'
}
]
})
cases: CaseExtensionEntity[];
}
@Entity({
name: 'case_extension_entity'
})
export class CaseExtensionEntity {
@PrimaryColumn()
id: string;
@ManyToOne(type => MeetingEntity, meetingEntity => meetingEntity.cases)
meetingEntity: MeetingEntity;
}
我在postgres中有3个表:
CREATE TABLE test.case_extension_entity
(
id character varying(255) NOT NULL,
CONSTRAINT case_extension_entity_pkey PRIMARY KEY (id)
)
CREATE TABLE test.meeting_entity
(
id character varying(255) NOT NULL,
CONSTRAINT meeting_entity_pkey PRIMARY KEY (id)
)
CREATE TABLE test.meeting_entity_cases
(
meeting_entity_id character varying(255) NOT NULL,
cases_id character varying(255) NOT NULL,
cases_order integer NOT NULL,
CONSTRAINT meeting_entity_cases_pkey PRIMARY KEY (meeting_entity_id, cases_order),
CONSTRAINT meeting_entity_cases_cases_id_fkey FOREIGN KEY (cases_id)
REFERENCES test.case_extension_entity (id) MATCH SIMPLE
ON UPDATE NO ACTION ON DELETE NO ACTION,
CONSTRAINT meeting_entity_cases_meeting_entity_id_fkey FOREIGN KEY (meeting_entity_id)
)
在这种情况下,当我尝试保存MeetingEntity时,出现以下错误消息:
错误:CaseExtensionEntity.meeting_entity_id列不存在
保存实体:
await getManager().transaction(async transactionalEntityManager => {
await transactionalEntityManager.save(meetingEntity);
});
当我从CaseExtensionEntity中删除@ManyToOne成员时,它正在工作,并且同时保存CaseExtensionEntity和MeetingEntity,但是在第3个表中没有任何内容(meeting_entity_cases)。但是我也想将Meeting_entity和case_extension_entity之间的关系也保存到Meeting_entity_cases中。
请你帮我一下吗?我想念什么?我花了第二天在Google上搜索,但没有成功。
编辑:我有这个旧数据库结构,供其他应用程序使用。双方更改为@ManyToMany解决了我的问题。可能(如果您保持理智),您不需要OneToMany关系的第三个关系表。