将MySQL自动递增ID设置为null

时间:2019-04-12 14:37:38

标签: mysql node.js sequelize.js

问题的症结在于Sequelize将我的插入对象的ID设置为null。我读过很多相关的问题,对于我的一生,我似乎无法弄清楚。

这是MySQL 5.7表的架构

CREATE TABLE `users` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(100) NOT NULL,
  `password` varchar(255) NOT NULL,
  `client_id` varchar(255) NOT NULL,
  `ipaddress` varchar(50) DEFAULT NULL,
  `enabled` tinyint(1) NOT NULL DEFAULT '1',
  `createdAt` datetime DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP,
  `updatedAt` datetime NOT NULL DEFAULT CURRENT_TIMESTAMP,
  `roles` json DEFAULT NULL,
  `provider` varchar(255) DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `users_id_uindex` (`id`),
  UNIQUE KEY `table_nameusers_username_uindex` (`username`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8;

这里是Sequelize模型,请注意我有primaryKey:true和autoIncrement:true

module.exports = function (sequelize, DataTypes) {
  return sequelize.define('User',
    {
      id: {
        type: DataTypes.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      username: {
        type: DataTypes.STRING,
      },
      password: DataTypes.STRING,
      client_id: DataTypes.STRING,
      ipaddress: DataTypes.STRING,
      enabled: DataTypes.BOOLEAN,
      roles: [],
      provider: DataTypes.STRING,
    });
};

这是您尝试再次插入时引发的错误。注意ID:null

{
    "type": "error",
    "message": "PRIMARY must be unique",
    "error": {
        "name": "SequelizeUniqueConstraintError",
        "errors": [
            {
                "message": "PRIMARY must be unique",
                "type": "unique violation",
                "path": "PRIMARY",
                "value": "0",
                "origin": "DB",
                "instance": {
                    "id": null,
                    "username": "testuser",
                    "password": "<password string>",
                    "roles": [
                        "user",
                        "mysql"
                    ],
                    "provider": "SOMETHING",
                    "client_id": "",
                    "enabled": true,
                    "updatedAt": "2019-04-12T15:08:44.068Z",
                    "createdAt": "2019-04-12T15:08:44.068Z"
                },
                "validatorKey": "not_unique",
                "validatorName": null,
                "validatorArgs": []
            }
        ],
        "fields": {
            "PRIMARY": "0"
        },
        "parent": {
            "code": "ER_DUP_ENTRY",
            "errno": 1062,
            "sqlState": "23000",
            "sqlMessage": "Duplicate entry '0' for key 'PRIMARY'",
            "sql": "INSERT INTO `Users` (`id`,`username`,`password`,`client_id`,`enabled`,`roles`,`provider`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);"
        },
        "original": {
            "code": "ER_DUP_ENTRY",
            "errno": 1062,
            "sqlState": "23000",
            "sqlMessage": "Duplicate entry '0' for key 'PRIMARY'",
            "sql": "INSERT INTO `Users` (`id`,`username`,`password`,`client_id`,`enabled`,`roles`,`provider`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);"
        },
        "sql": "INSERT INTO `Users` (`id`,`username`,`password`,`client_id`,`enabled`,`roles`,`provider`,`createdAt`,`updatedAt`) VALUES (DEFAULT,?,?,?,?,?,?,?,?);"
    }
}

预期结果:id字段是下一个自动递增的id。

实际结果:数据库中的id字段始终为0,在Sequelize对象中为null。

偶然地,我通过手动创建表并允许Sequelize在sync()期间创建表来运行它。

0 个答案:

没有答案