【问题标题】:Running Sql migrations throws syntax error运行 Sql 迁移会引发语法错误
【发布时间】:2021-11-26 22:12:18
【问题描述】:

我已经使用 sequelize-cli 创建了我的第一个迁移,现在当我输入 npx sequelize-cli db:migrate 运行迁移并在数据库中创建表时,我收到错误 我查看文档找不到迁移文件的方式和内容。

错误

ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'NUMBER, `otp` INTEGER, `otp_expiration_date` DATETIME, `createdAt` DATETIME NOT ' at line 1

我的迁移文件

'use strict';
module.exports = {
    up: async (queryInterface, Sequelize) => {
        await queryInterface.createTable('Users', {
            id: {
                allowNull: false,
                autoIncrement: true,
                primaryKey: true,
                type: Sequelize.INTEGER
            },
            name: {
                type: Sequelize.STRING
            },
            phone_number: {
                type: Sequelize.NUMBER
            },
            otp: {
                type: Sequelize.INTEGER(4)
            },
            otp_expiration_date: {
                type: Sequelize.DATE
            },
            createdAt: {
                allowNull: false,
                type: Sequelize.DATE
            },
            updatedAt: {
                allowNull: false,
                type: Sequelize.DATE
            }
        })
    },
    down: async (queryInterface, Sequelize) => {
        await queryInterface.dropTable('Users');
    }
};

我的用户模型

const moment = require('moment');

'use strict';
module.exports = (sequelize, DataTypes) => {
    class User extends Model {
        /**
         * Helper method for defining associations.
         * This method is not a part of Sequelize lifecycle.
         * The `models/index` file will call this method automatically.
         */
        static associate(models) {
            // define association here
        }
    };
    User.init({
        name: {
            type: DataTypes.STRING,
            allowNull: false
        },
        phone_number: {
            type: DataTypes.NUMBER
        },
        otp: {
            type: DataTypes.INTEGER(4)
        },
        otp_expiration_date: {
            type: DataTypes.DATE,
            set(value) {
                // convert regular Date to moment Date
                value = moment(value).add(5, 'minutes');
                this.setDataValue('otp_expiration_date', value);
            }
        },
        is_otp_expired: {
            type: DataTypes.VIRTUAL,
            get() {
                // otp_expiration_date < current date
                return this.getDataValue(otp_expiration_date).isAfter(moment()) ? true : false
            }
        }
    }, {
        sequelize,
        modelName: 'User',
    });
    return User;
};

我试过了

  • 改变数据类型
  • 将吸气剂转移到迁移中
  • 删除 createdAt

【问题讨论】:

    标签: mysql sequelize.js migration database-migration sequelize-cli


    【解决方案1】:

    [已解决]

    问题在于 phone_number 的 DataType ,每个 Sequelize docs 没有 Sequelize.NUMBER 类型

    我将 Sequelize DataTypes 与 MySql DataTypes 混淆了

    phone_number: {
            type: DataTypes.NUMBER   <---- bug
        }
    

    解决方案

    phone_number: {
            type: DataTypes.INTEGER <---- solution
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-07-14
      • 2022-10-05
      • 2019-09-27
      • 2018-06-29
      • 2021-07-29
      • 2016-08-27
      • 1970-01-01
      • 2017-01-06
      相关资源
      最近更新 更多