Sequelize CLI:db:migrate 导致大多数列无法添加

Sequelize CLI: db:migrate results in most columns failing to add

// package.json
    "pg": "^8.5.1",
    "sequelize": "^6.5.0",
    "sequelize-cli": "^6.2.0"

// terminal
$ node --version
v14.15.4

这是我在运行 npx sequelize db:migrate.

之后看到的整个迁移文件
'use strict';
module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('Documents', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      id: {
        type: Sequelize.INTEGER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
    });
  },
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('Documents');
  }
};

数据库table 使用 DBeaver 检查只显示一个 id 列,而不是迁移文件中看到的重复项。另外两个总是添加的列。其他一切都不见了。 https://i.imgur.com/FvsyAAN.png

./models 下只有一个文件 document.js,也是由 CLI 创建的。我已经将其更新为:

'use strict';
const { Model } = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class Document extends Model {
    static associate(models) {
    }
  };
  Document.init({
    id: {
      type: DataTypes.uuid4,
      defaultValue: DataTypes.uuid4,
      primaryKey: true,
      allowNull: false,
    },
    ownerDoc: {
      type: DataTypes.String,
      allowNull: false,
    },
    ownerName: {
      type: DataTypes.String,
      allowNull: false,
    },
    createdAt: DataTypes.Date,
    updatedAt: DataTypes.Date,
    deletdAt: {
      type: DataTypes.Date,
      allowNull: true,
    },
    uploadBy: {
      type: DataTypes.String, 
      allowNull: true,
    },
    fileUrl: {
      type: DataTypes.String,
      allowNull: true,
    },
    category: {
      type: DataTypes.String,
      allowNull: false,
    },
    status: {
      type: DataTypes.String,
      allowNull: false,
    },
    fileType: {
      type: DataTypes.String,
      allowNull: true,
    }, 
    version: {
      type: DataTypes.number,
      allowNull: false,
    },
    type: {
      type: DataTypes.String,
      allowNull: false,
    },
    source: {
      type: DataTypes.String,
      allowNull: false,
    },
    data: {
      type: DataTypes.JSON,
      allowNull: true,
    }, // any[],
    expiration: {
      type: DataTypes.Date,
      allowNull: true,
    },
    requestAgainExpiration: {
      type: DataTypes.number,
      allowNull: true,
    },
    titleSufix: {
      type: DataTypes.String,
      allowNull: true,
    }, 
    inputRequest: {
      type: DataTypes.JSON,
      allowNull: true,
    }, // any[],
    parentId: {
      type: DataTypes.String,
      allowNull: true,
    },
    borrowerId: {
      type: DataTypes.String,
      allowNull: true,
    },
    loanApplicationId: {
      type: DataTypes.String,
      allowNull: true,
    },
  }, {
    sequelize,
    modelName: 'Document',
  });
  return Document;
};

这是 CLI 输出

sean@desktop:~/cp/caBackend$ npx sequelize db:migrate

Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]

Loaded configuration file "config/config.js".
Using environment "development".
No migrations were executed, database schema was already up to date.


sean@desktop:~/cp/caBackend$ npx sequelize db:migrate

Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]

Loaded configuration file "config/config.js".
Using environment "development".
No migrations were executed, database schema was already up to date.
sean@desktop:~/cp/caBackend$ 

有人能说出迁移无法从模型文件正确更新的原因吗?

尝试过

// migration file, same one as shown above
'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => Promise.all([
    queryInterface.addColumn('Documents', 'ownerDoc', { type: Sequelize.String, allowNull: false }),
    queryInterface.addColumn('Documents', 'ownerName', { type: Sequelize.String, allowNull: false }),
    queryInterface.addColumn('Documents', 'deletdAt', { type: Sequelize.DATE, allowNull: true }),
    queryInterface.addColumn('Documents', 'deletdAt', { type: Sequelize.DATE, allowNull: true }),
    queryInterface.addColumn('Documents', 'uploadBy', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'fileUrl', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'category', { type: Sequelize.STRING, allowNull: false }),
    queryInterface.addColumn('Documents', 'status', { type: Sequelize.STRING, allowNull: false }),
    queryInterface.addColumn('Documents', 'fileType', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'version', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'type', { type: Sequelize.STRING, allowNull: false }),
    queryInterface.addColumn('Documents', 'source', { type: Sequelize.STRING, allowNull: false }),
    queryInterface.addColumn('Documents', 'data', { type: Sequelize.JSON, allowNull: true }),
    queryInterface.addColumn('Documents', 'expiration', { type: Sequelize.Date, allowNull: true }),
    queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: Sequelize.NUMBER, allowNull: true }),
    queryInterface.addColumn('Documents', 'titleSufix', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'inputRequest', { type: Sequelize.JSON, allowNull: false }),
    queryInterface.addColumn('Documents', 'parentId', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'borrowerId', { type: Sequelize.STRING, allowNull: true }),
    queryInterface.addColumn('Documents', 'loanApplicationId', { type: Sequelize.STRING, allowNull: true }),
  ]),
  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('Documents');
  },
};

npx sequelize db:migratenpx sequelize-cli db:migrate 都显示:

No migrations were executed, database schema was already up to date.

sean@desktop:~/cp/caBackend$ npx sequelize migration:create --name alter-documetns

'use strict';
module.exports = {
    up: (queryInterface, Sequelize) => {
        return Promise.all([
            queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }),
            queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }),
            queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
            queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
            queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}),
            
            queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}),
            queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}),
            queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}),
            queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}),
        ])
    },
    down: (queryInterface, Sequelize) => {

    }
};

npx sequelize migration:create --name alter-documents

然后用上面的代码替换默认值:

$ npx sequelize db:migrate

Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]

Loaded configuration file "config/config.js".
Using environment "development".
== 20210306190146-alter-documents: migrating =======

ERROR: Cannot read property 'toString' of undefined

sean@desktop:~/cp/caBackend$ npx sequelize migration:create --name alter-documetns

'use strict';
module.exports = {
    up: (queryInterface, DataTypes) => {
        return Promise.all([
            queryInterface.addColumn('Documents', 'ownerDoc', { type: DataTypes.String, allowNull: false, }),
            queryInterface.addColumn('Documents', 'ownerName', { type: DataTypes.String, allowNull: false, }),
            queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
            queryInterface.addColumn('Documents', 'deletdAt', { type: DataTypes.DATE, allowNull: true}),
            queryInterface.addColumn('Documents', 'uploadBy', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'fileUrl', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'category', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'status', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'fileType', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'version', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'type', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'source', { type: DataTypes.STRING, allowNull: false}),
            queryInterface.addColumn('Documents', 'data', { type: DataTypes.JSON, allowNull: true}),
            
            queryInterface.addColumn('Documents', 'expiration', { type: DataTypes.Date, allowNull: true}),
            queryInterface.addColumn('Documents', 'requestAgainExpiration', { type: DataTypes.NUMBER, allowNull: true}),
            queryInterface.addColumn('Documents', 'titleSufix', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'inputRequest', { type: DataTypes.JSON, allowNull: false}),
            queryInterface.addColumn('Documents', 'parentId', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'borrowerId', { type: DataTypes.STRING, allowNull: true}),
            queryInterface.addColumn('Documents', 'loanApplicationId', { type: DataTypes.STRING, allowNull: true}),
        ])
    },
    down: (queryInterface, Sequelize) => {

    }
};

npx sequelize migration:create --name alter-documents

然后用上面的代码替换默认值:

$ npx sequelize db:migrate

Sequelize CLI [Node: 14.15.4, CLI: 6.2.0, ORM: 6.5.0]

Loaded configuration file "config/config.js".
Using environment "development".
== 20210306190146-alter-documents: migrating =======

ERROR: Cannot read property 'toString' of undefined