ORM model self-referencing relation (parent / children) #114
-
I've been trying to work out how to make a self-referencial model, as the title suggest for the purposes of a parent / children structure. i.e. @entity.name('page').collectionName('pages')
export class Page {
@t.primary.autoIncrement public id: number = 0
@t.number private parentId?: number
// @t.reference({ foreignKey: 'parentId' ) public parent: Page
// @t.array(() => Page).backReference() public children?: Page[]
} I realise the parentId field is doing nothing in the above example and the references are completely invalid but I'm used to being able to specify an association with a foreign key and couldn't find anything in the docs or type definition comments on how to achieve something like this. Cheers! |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
You would specify the entity like that: @entity.name('page').collectionName('pages')
export class Page {
@t.primary.autoIncrement public id: number = 0
@t.reference() public parent?: Page;
@t.array(() => Page).backReference() public children?: Page[];
} This would create CREATE TABLE `pages` (
`id` double NOT NULL AUTO_INCREMENT,
`parent` double NULL,
PRIMARY KEY (`id`),
CONSTRAINT `pages_fkb8c9730096d221d4`
FOREIGN KEY (`parent`)
REFERENCES `pages` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
CREATE INDEX `pages_fkb8c9730096d221d4` ON `pages` (`parent`) |
Beta Was this translation helpful? Give feedback.
You would specify the entity like that:
This would create