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: Validation error

See original GitHub issue

What you are doing?

when i try insert a record into an empty table I get this message:

SequelizeUniqueConstraintError: Validation error

errors: [ { message: ‘PRIMARY must be unique’, type: ‘unique violation’, path: ‘PRIMARY’, value: ‘2-50-100-431439-1’ } ], fields: { PRIMARY: ‘2-50-100-431439-1’ }, parent: { Error: ER_DUP_ENTRY: Duplicate entry ‘2-50-100-431439-1’ for key ‘PRIMARY’

When I run the insert generated by sequelize, the record is successfully inserted. INSERT INTO contrato.pbms (TPO_PBMS,CLS_PBMS,SCL_PBMS,SEQ_PBMS,CD_SIS_OGM,NOM_ITEM,IND_BASE,CD_TIP_ITEM) VALUES (2,50,100,431439,1,'MANUT/SUP SOLUC VIDEOWALL ',20,99);

Post a minimal code sample that reproduces the issue, including models and associations Model :

var pbms = sequelize.define('pbms', {

        TPO_PBMS: {
            type: DataTypes.INTEGER,
            underscored: true,
            allowNull: false,
            primaryKey: true
        },
        CLS_PBMS: {
            type: DataTypes.INTEGER,
            underscored: true,
            allowNull: false,
            primaryKey: true
        },
        SCL_PBMS: {
            type: DataTypes.INTEGER,
            underscored: true,
            allowNull: false,
            primaryKey: true
        },
        SEQ_PBMS: {
            type: DataTypes.INTEGER,
            underscored: true,
            allowNull: false,
            primaryKey: true
        },
        CD_SIS_OGM: {
            type: DataTypes.INTEGER,
            underscored: true,
            allowNull: false,
            primaryKey: true
        },
        NOM_ITEM: {
            type: DataTypes.STRING(50),
            underscored: true,
            allowNull: true
        },
        IND_BASE: {
            type: DataTypes.INTEGER,
            allowNull: true,
            underscored: true
        },
        CD_TIP_ITEM: {
            type: DataTypes.INTEGER,
            underscored: true,
            allowNull: true
        }
    }, {
        schema   : 'contrato',
        tableName: 'pbms',
        freezeTableName: true,
        classMethods: {
        }
    });

Data

var data = {
            'TPO_PBMS' : 2,
            'CLS_PBMS' : 50,
            'SCL_PBMS' : 100,
            'SEQ_PBMS' : 431439,
            'CD_SIS_OGM' : 1,
            'NOM_ITEM' :        'MANUT/SUP SOLUC VIDEOWALL          ',
            'IND_BASE' : 20,
            'CD_TIP_ITEM' : 99
        };

Table CREATE TABLEpbms( TPO_PBMSmediumint(6) NOT NULL, CLS_PBMSsmallint(6) NOT NULL, SCL_PBMSsmallint(6) NOT NULL, SEQ_PBMSint(11) NOT NULL, CD_SIS_OGMsmallint(6) NOT NULL, NOM_ITEMvarchar(50) DEFAULT NULL, IND_BASEsmallint(6) DEFAULT NULL, CD_TIP_ITEMsmallint(6) DEFAULT NULL, PRIMARY KEY (TPO_PBMS,CLS_PBMS,SCL_PBMS,SEQ_PBMS,CD_SIS_OGM) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;

What do you expect to happen?

insert the record

What is actually happening?

error

Output, either JSON or SQL

Dialect: mysql Database version: 5.6 Sequelize version: last version

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

3reactions
Andersonfrfilhocommented, Apr 14, 2020

the error happened to me in this case was the postbird, which was with the next id that already existed even with the sequelize in auto_incremete configuration when executing the 1st command you can see where the id is in the postbird and the second vc sets the id value, I hope I helped no postbird in query run: (table name, field name)

select nextval ('contacts_id_seq')
select setval('phones_id_seq', 91)
0reactions
agueroveraalvarocommented, Jun 6, 2020

Try with unique: false in relation code

MyModel(OtherModel, { foreignKey: { name:'myModel_id', unique: false}, as: 'otherModels' });

Read more comments on GitHub >

github_iconTop Results From Across the Web

Unhandled rejection SequelizeUniqueConstraintError
The call to User.create() is returning a Promise.reject() , but there is no .catch(err) to handle it. Without catching the error and knowing ......
Read more >
Unhandled rejection SequelizeUniqueConstraintError ...
MySQL : Unhandled rejection SequelizeUniqueConstraintError : Validation error [ Beautify Your Computer ...
Read more >
Silent validation error with Sequelize - Help me!
In my sequelize model, I have a unique constraint (reflects the UNIQUE constraint in Postgres). Problem is, when in ForestAdmin I perform an ......
Read more >
Validations & Constraints - Sequelize
Model validator methods are called with the model object's context and are deemed to fail if they throw an error, otherwise pass. This...
Read more >
[Solved]-Unhandled rejection SequelizeUniqueConstraintError
Coding example for the question Unhandled rejection SequelizeUniqueConstraintError: Validation error-node.js.
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