question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

SequelizeUniqueConstraintError on child creation

See original GitHub issue

This 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:closed
  • Created 7 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
Varkalcommented, Jun 26, 2016

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.

1reaction
overlookmotelcommented, Jun 25, 2016

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 AccountHierarchy table.

Two possibilities for why this is happening:

  1. A bug in this module
  2. AccountHierarchy table 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:

  • Before running that test, manually delete or empty the AccountHierarchy table from the database, and see if you still get the same error.
  • Ensure that models.sequelize.sync({force: true}) is syncing all models, including AccountHierarchy. From your code, I can’t tell whether models.sequelize refers to the the db object that Account model 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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Creating new instances for a Many to Many relationship using ...
The error is that you created two instances of social that have the same id, which should be unique. The correct way to...
Read more >
[Solved]-Unhandled rejection SequelizeUniqueConstraintError
In case you stumble upon this Validation error from Sequelize: check that the query populating (creating) the table is performed once.
Read more >
Upgrading ACE to 3.4.0 results in ...
I'm facing “SequelizeUniqueConstraintError” during the schema sync ... in SequelizeUniqueConstraintError on AddonSettings Index creation.
Read more >
cannot add foreign key constraint mysql
... to JavaScript (in this example, throwing a SequelizeUniqueConstraintError ). ... This is useful to guarantee that a child will only be created...
Read more >
Untitled
I have created an entity data Model for Sequelize, as follows. ... error message : "Unhandled rejection SequelizeUniqueConstraintError: Validation error".
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found