【发布时间】:2015-09-04 18:32:00
【问题描述】:
我有一个组织树,并希望确保在树的同一级别(甚至在根级别)中只有一个组织(使用其name 和parent_id)。
例如:
有效树:
/org1 /org2
/org1/org2
/org3
无效树:
/org1
/org1
/org1/org2
/org2
型号:
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
});
我尝试了以下方法,但没有任何结果:
{
comment : 'An organization.',
hierarchy: true,
indexes : [
{
unique: true,
fields: ['parent_id', 'name']
}
],
}
更新:
我将问题范围缩小到parent_id 为 NULL 时的情况,因此当其中一个值为 NULL 时,唯一复合索引似乎不起作用。
那么,有办法解决这个问题吗?
提前致谢。
【问题讨论】:
-
看看这个,不确定这是否正是你想要的,但看起来很相似 - stackoverflow.com/questions/26006724/…
-
@Bulat 感谢您的评论。不,可悲的是,这不是我遇到的问题 :( 你能重新审视我对问题所做的更新的问题吗?谢谢!
-
您使用的是哪个数据库?
-
@ezrepotein sqlite 和 mysql,但现在我正在测试 sqlite
-
@diosney 这可能是您现在面临的问题:stackoverflow.com/questions/22699409/sqlite-null-and-unique 吗?
标签: javascript sql node.js orm sequelize.js