Skip to content

ORM model self-referencing relation (parent / children) #114

Answered by marcj
jacobclarke92 asked this question in Q&A
Discussion options

You must be logged in to vote

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`)

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jacobclarke92
Comment options

Answer selected by jacobclarke92
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants