-
Notifications
You must be signed in to change notification settings - Fork 369
Closed
Description
Trying to solve tree heirarchy through orm.
CREATE TABLE `category` (
`id` int(11) NOT NULL,
`name` varchar(1024) COLLATE utf8_czech_ci NOT NULL,
PRIMARY KEY (`id`)
);
CREATE TABLE `category_tree` (
`predecessor_id` int(11) NOT NULL,
`successor_id` int(11) NOT NULL,
`distance` int(11) NOT NULL,
PRIMARY KEY (`predecessor_id`,`successor_id`),
KEY `FK_category_tree_successor` (`successor_id`),
CONSTRAINT `FK_category_tree_predecessor` FOREIGN KEY (`predecessor_id`) REFERENCES `category` (`id`),
CONSTRAINT `FK_category_tree_successor` FOREIGN KEY (`successor_id`) REFERENCES `category` (`id`)
);Defined model
var db = config.db;
var Category = db.define('category', {
//properties
id : Number,
name : String
},{
//options
});
Category.hasMany("successors", {distance : Number}, Category, {reverse: 'predecessors', mergeTable: 'category_tree', mergeId: 'predecessor_id', mergeAssocId: 'successor_id'});Executing this:
Category(6).getSuccessors({distance: 1}, ['name', 'A'], function(err, categories) {
res.json(categories);
}); generates:
SELECT * FROM `category` WHERE `distance` = 1 AND `category_tree`.`predecessor_id` = 6 ORDER BY `name` ASC Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels