javascript - 如何从 Sequelize 模型自动生成迁移

标签 javascript node.js sequelize.js sequelize-cli automatic-migration

我刚开始用 node.js 编程。我需要一个 ORM,所以我使用了 Sequelize 库。我按如下方式编写了我的用户模型,我需要为它创建一个迁移。有没有办法从 Sequelize 模型自动创建迁移而不是自己编写它们?
我试过包 sequelize-auto-migrations 并运行 npx makemigration --name create_users 但它返回错误:

Unexpected token {
我正在使用 Node v10.19.3
这是我的 Sequelize 模型:

const Model = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class User extends Model {
    getFullname() {
      return [ this.firstname, this.lastname ].join(' ');
    }
  }
  User.init({
    firstName: { type: DataTypes.STRING, allowNull: false },
    lastName: { type: DataTypes.STRING, allowNull: false },
    email: { type: DataTypes.STRING, allowNull: false, unique: true },
    national_number: { type: DataTypes.STRING, unique: true },
    phone_number: { type: DataTypes.STRING, allowNull: false, unique: true },
    username: { type: DataTypes.STRING, allowNull: false, unique: true },
    join_date: { type: DataTypes.DATEONLY, defaultValue: sequelize.NOW },
    last_login: { type: DataTypes.DATE },
    password: { type: DataTypes.STRING, allowNull: false },
    active: { type: DataTypes.BOOLEAN, allowNull: false, defaultValue: true }
  }, {
    sequelize,
    modelName: 'User',
  });
  return User;
};

最佳答案

您可能想尝试使用 sequelize-cli。要运行迁移,您必须运行此命令 npx sequelize-cli model:generatesequelize.com 上的这个链接应该对 https://sequelize.org/master/manual/migrations.html 有所帮助

关于javascript - 如何从 Sequelize 模型自动生成迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65164499/

相关文章:

javascript - Java Nashorn 中的(取消)引用方法变体

javascript - 将内容从一个元素复制到另一个元素

javascript - Mongoose :聚合和计数

node.js - node-gyp 在安装过程中失败 web3 scrypt@6.0.3 install : `node-gyp rebuild`

node.js - 我可以使用 VSCode 在本地运行/调试 Heroku Node.js 应用程序吗?

node.js - Sequelize数据库错误: permission denied for relation notice_opened_tbl

sql - Sequelize : join two times the same table

javascript - 浏览器在上传到服务器时找不到背景图像?

node.js - 按日期排序包含多个日期的列表

node.js - 一饮而尽 : Cannot delete files outside the current working directory