迁移时node.js 没有主键续集

Jon*_*bek 3 database migrate sequelize.js

这是我create_user_table在 Node.js 项目中的迁移。

'use strict';

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable("comments", {
      content: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      lastName: {
        type: Sequelize.STRING(20),
        allowNull: false
      }
    })
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable("comments")
  }
};
Run Code Online (Sandbox Code Playgroud)

运行sequelize db:migrate后,结果如下:

sequelize db:migrate

Sequelize CLI [Node: 10.15.2, CLI: 5.4.0, ORM: 5.8.5]

Loaded configuration file "config/config.json".
Using environment "development".
== 20190507193540-create_comment_table: migrating =======
== 20190507193540-create_comment_table: migrated (0.021s)

== 20190507195058-create_user_table: migrating =======
== 20190507195058-create_user_table: migrated (0.011s)
Run Code Online (Sandbox Code Playgroud)

当我describe users;在 mysql CLI 中运行时,表不包含 ID?

   +----------+-------------+------+-----+---------+-------+
    | Field    | Type        | Null | Key | Default | Extra |
    +----------+-------------+------+-----+---------+-------+
    | username | varchar(20) | NO   |     | NULL    |       |
    | lastName | varchar(20) | NO   |     | NULL    |       |
    +----------+-------------+------+-----+---------+-------+
Run Code Online (Sandbox Code Playgroud)

为了完全确定,我也尝试过。

mysql> show index from users where id = 'PRIMARY';
ERROR 1054 (42S22): Unknown column 'id' in 'where clause'
Run Code Online (Sandbox Code Playgroud)

mcr*_*n18 6

id您必须在模型和迁移文件中手动声明一个字段,例如:

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable("comments", {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      content: {
        type: Sequelize.STRING(20),
        allowNull: false
      },
      lastName: {
        type: Sequelize.STRING(20),
        allowNull: false
      }
    })
  },

  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable("comments")
  }
};
Run Code Online (Sandbox Code Playgroud)