I've an organizations' tree and want to ensure that there is only one organization (using its name
and parent_id
) at the same level in the tree, even at the root level.
For instance:
valid trees:
/org1 /org2
/org1/org2
/org3
invalid trees:
/org1
/org1
/org1/org2
/org2
Model:
var Organization = global.erp.orm.sequelize.define('Organization',
lodash.extend({}, global.erp.orm.mixins.attributes, {
name : {
type : global.erp.orm.Sequelize.STRING,
comment : 'The organization short name (its common referred name).',
allowNull: false
}
}), {
comment : 'An organization.',
hierarchy: true
});
I've tried the following but without any results:
{
comment : 'An organization.',
hierarchy: true,
indexes : [
{
unique: true,
fields: ['parent_id', 'name']
}
],
}
UPDATE:
I narrowed the issue to the case that when parent_id
is NULL, so it seems that unique composite indexes don't work when one of the values is NULL.
So, there is a solution to this?
Thanks in advance.