overlookmotel / sequelize-hierarchy

Nested hierarchies for Sequelize
MIT License
302 stars 90 forks source link

Need help with insertion #226

Closed dcromster closed 3 years ago

dcromster commented 4 years ago

Hello!

I didn't find an example of correct data insertion in the documentation, and that's why I'm suffering. On insertion, I get "(node: 31110) UnhandledPromiseRejectionWarning: SequelizeUniqueConstraintError: Validation error"

My insertion: await configDataTable.create({ serviceID: 1, keyName: 'Root', keyValue: '', KeyType: 2, // hierarchyLevel: 1, parentId: 1 });

does not work without parentId either.

It inserts once, and then no more rows can be inserted into the table.

Table:

const configDataTable = sequelize.define('configData', {
  serviceID: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  },
  keyName :{
    type: DataTypes.STRING,
    allowNull: false,
    defaultValue: ''
  },
  keyValue: {
    type: DataTypes.STRING,
    allowNull: true,
    defaultValue: ''
  },
  keyType: {
    type: DataTypes.INTEGER,
    defaultValue: 0
  }
});
configDataTable.isHierarchy();
overlookmotel commented 4 years ago

Do you have a record already in the table with id of 1? NB id will be the primary key for the table, not ServiceID.

If that's not the cause, please try removing the line configDataTable.isHierarchy() and parentId: 1 from the query and see if the query still fails. If so, then the error has nothing to do with sequelize-hierarchy and is some other constraint failure.

NB hierarchyLevel does not need to be specified in your query. sequelize-hierarchy will fill that field automatically for you.

Let me know if that helps.

dcromster commented 4 years ago

Thanks for the quick response!

Yes, I have one record with id = 1. This record was inserted normally. Yes, id the primary key.

Found an error in the database: auto-increment was not set on id.

But now I got new error when I try to insert second row: await configDataTable.create({ serviceID: 1, keyName: 'Root2', keyValue: '', KeyType: 2, // hierarchyLevel: 1, // aclCreator: 'n', // aclGroup: 'n', // aclOthers:'n', parentId: 1 });

UnhandledPromiseRejectionWarning: SequelizeDatabaseError: Table 'configDataancestors' doesn't exists.

This part of documentation I didn't understand well too. How create this table?

overlookmotel commented 3 years ago

Please see here in the docs for solution.