SequelizeUniqueConstraintError: Validation error
See original GitHub issueWhat 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 TABLE
pbms(
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:
- Created 7 years ago
- Comments:5 (1 by maintainers)
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)
Try with
unique: false
in relation codeMyModel(OtherModel, { foreignKey: { name:'myModel_id', unique: false}, as: 'otherModels' });