SequelizeUniqueConstraintError on child creation
See original GitHub issueThis simple test throw a SequelizeUniqueConstraintError
beforeEach((done) => {
models.sequelize.sync({force: true}).then(() => {
this.Account = models.Account;
done();
});
});
it('shoud handle correctly hierarchy of account', () => {
var parentAccount = {
title : "Parent Account",
balance: 0
};
var childAccount = {
title : "Child Account",
balance: 0
};
return this.Account.create(parentAccount)
.then((parentAccountCreated) => {
childAccount.parentId = parentAccountCreated.id;
return this.Account.create(childAccount)
})
.then(
(childAccountCreated) => {
console.log(childAccountCreated.parentId);
},
(error) => {
console.log(error);
}
)
});
But, after exec it, in MySQL i dound those datas :
| id | title | balance | deleted | deletion_date | creation_date | modification_date | hierarchyLevel | parentId | ±—±---------------±--------±--------±--------------±--------------------±--------------------±------------+ | 1 | Parent Account | 0 | NULL | NULL | 2016-06-25 13:51:35 | 2016-06-25 13:51:35 | 1 | NULL | | 2 | Child Account | 0 | NULL | NULL | 2016-06-25 13:51:35 | 2016-06-25 13:51:35 | 2 | 1 |
Like everything is ok.
So i don’t understand where is my mistake…
Do you have any idea ?
Some additional infos :
The detailed error :
{ [SequelizeUniqueConstraintError: Validation error]
name: 'SequelizeUniqueConstraintError',
message: 'Validation error',
errors:
[ { message: 'PRIMARY must be unique',
type: 'unique violation',
path: 'PRIMARY',
value: '2-1' } ],
fields: { PRIMARY: '2-1' } }
The correspondig model :
var Account = db.define("Account", {
title : {
type: Sequelize.STRING
},
balance : {
type: Sequelize.FLOAT
},
deleted : {
type: Sequelize.BOOLEAN,
},
deletion_date: {
type: Sequelize.DATE
}
},
{
timestamps : true,
createdAt : "creation_date",
updatedAt : "modification_date",
hierarchy : true
});
return Account;
Issue Analytics
- State:
- Created 7 years ago
- Comments:10 (6 by maintainers)

Top Related StackOverflow Question
Thanks you so much !
I think it could be a useful to write somewhere that an user can check if sequelize-hierarchy is already loaded by check the presence of the
Sequelize.HierarchyError.Thanks for raising this.
When you set a hierarchy on a model, it also defines another model to track the descendents of all nodes. In your case this would be called
AccountHierarchy.It looks like the error is coming from trying to insert a duplicated row into
AccountHierarchytable.Two possibilities for why this is happening:
AccountHierarchytable is not being cleared before the test run so row inserted in a previous test or previous run of this test are still present.I suspect it’s (2) as there are already tests for this which are passing fine.
To narrow this down, I’d suggest the following:
AccountHierarchytable from the database, and see if you still get the same error.models.sequelize.sync({force: true})is syncing all models, includingAccountHierarchy. From your code, I can’t tell whethermodels.sequelizerefers to the thedbobject thatAccountmodel is being defined on.Please can you try this and let me know? If there’s a bug, of course I want to fix it ASAP, but I suspect the fault is with your code somewhere.