I’m new to typeorm and typescript in general, coming from python sqlalchemy and django models.
What I’m trying to create is in essence a doubly linked list in typeorm.
I’m looking for a way to update the child relation of a parent field when creating a child related to the parent.
Entity:
@Entity()
export class StoryNode {
@PrimaryGeneratedColumn()
id: number;
@Column()
name: string;
@Column('text')
text: string;
@Column({ type: "int", nullable: true })
prevNodeId: number;
@OneToOne(
() => StoryNode, (storynode) => storynode.nextNode,
{ nullable: true }
)
@JoinColumn({ name: "prevNodeId" })
prevNode: StoryNode;
@Column({ type: "int", nullable: true })
nextNodeId: number;
@OneToOne(
() => StoryNode, (storynode) => storynode.prevNode,
{ nullable: true, cascade: true }
)
@JoinColumn()
nextNode: StoryNode;
}
The entities are created from the following jsons.
Parent:
{
"name": "Node 1",
"text": "string"
}
Child:
{
"name": "Node 2",
"text": "string",
"prevNodeId": 1
}
Getting entities after creation:
[
{
"id": 1,
"name": "Node 1",
"text": "string",
"prevNodeId": null,
"nextNodeId": null
},
{
"id": 2,
"name": "Node 2",
"text": "string",
"prevNodeId": 1,
"nextNodeId": null
}
]
I tried googling related issues but couldn’t find any. When adding the relation to my find option i can se that the relation works on the child, but is still not updated on the parent.
I would expect that after creating Node 2 with prevNodeId set to Node 1 id, that the nextNodeId would be updated to the id of Node 2.
Am i correct in expecting this behavior and how do i fix it, or should i handle it somehow differently?