V
V
Vladimir2019-10-19 04:23:17
Node.js
Vladimir, 2019-10-19 04:23:17

Why doesn't autoincrement generate an id?

Good morning. Please tell me why the specified parameter does not work? I took the example from the documentation, substituted my fields.

const sequelize = new Sequelize('test', 'root', '', {
  host: 'localhost',
  dialect: 'mysql'
});

sequelize
  .authenticate()
  .then(() => {
    console.log('Connection has been established successfully.');
  })
  .catch(err => {
    console.error('Unable to connect to the database:', err);
  });

const User = sequelize.define('users', {
  // attributes
  id: {
    type: Sequelize.INTEGER,
    primaryKey: true,
    autoIncrement: true //Должен генерировать id
    // defaultValue: 123 - пределение id в ручную,но в документации этого не было,я это выгуглил
  },
  email: {
    type: Sequelize.STRING,
    allowNull: false
  },
  password: {
    type: Sequelize.STRING
    // allowNull defaults to true
  }
}, {
  // options
});

User.create({ email: "Jane", password: "Doe" }).then(jane => {
  console.log("Jane's auto-generated ID:", jane.id);
});


Unhandled rejection SequelizeUniqueConstraintError: Validation error
at Query.formatError (C:\Users\History\Desktop\session\node_modules\sequelize\lib\dialects\mysql\query.js:223:16)
at Execute.handler [as onResult] (C :\Users\History\Desktop\session\node_modules\sequelize\lib\dialects\mysql\query.js:51:23)
at Execute.execute (C:\Users\History\Desktop\session\node_modules\mysql2\lib\ commands\command.js:30:14)
at Connection.handlePacket (C:\Users\History\Desktop\session\node_modules\mysql2\lib\connection.js:408:32)
at PacketParser.Connection.packetParser.p [as onPacket] (C:\Users\History\Desktop\session\node_modules\mysql2\lib\connection.js:70:12)
at PacketParser.executeStart (C:\Users\History\Desktop\session\node_modules\mysql2\lib\packet_parser.js:75:16)
at Socket.Connection.stream.on.data (C:\Users\History\Desktop\ session\node_modules\mysql2\lib\connection.js:77:25)
at Socket.emit (events.js:198:13)
at addChunk (_stream_readable.js:288:12)
at readableAddChunk (_stream_readable.js:269:11 )
at Socket.Readable.push (_stream_readable.js:224:10)
at TCP.onStreamRead [as onread] (internal/stream_base_commons.js:94:17)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir, 2019-10-19
@HistoryART

There was a conflict between v5 / v4 versions, I replaced the config with the config from the v4 docks and it all worked

A
Anton Neverov, 2019-10-19
@TTATPuOT

https://stackoverflow.com/questions/15737949/how-d... - don't create an id column and that's it. Sequelize will do it itself

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question