'How do I join two entities linked with 1-1 relationship and add the joined entity on a particular property of the first entity?
I have 2 entities with a one to one relationship between them created with typeorm.
@ObjectType()
@Entity()
export class Interaction extends BaseEntity {
@Field()
@PrimaryGeneratedColumn()
id!: number;
@Field(() => AF_Metadata)
@JoinColumn()
@OneToOne(() => AF_Metadata, { eager: true })
metadata: AF_Metadata;
}
}
@ObjectType()
@Entity()
export class AF_Metadata extends BaseEntity {
@Field()
@PrimaryGeneratedColumn()
id!: number;
//Relationships
@OneToOne(() => Interaction, { onDelete: "CASCADE" })
interaction: Interaction;
}
How do I join these entities by adding the AF_Metadata entity to the metadata property in the Interaction entity. I am able to add the whole AF_Metadata object to the interaction but not to the metadata propery. This is my sql query so far.
select * from interaction int
left join af_metadata metadata on int."metadataId" = metadata.id
order by int."createdAt" DESC
Current result:
{
id: 8327,
metadataId: 1,
}
Desired result:
{
id: 8327,
metadata: {
metadataId: 1
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
