【问题标题】:Sequelize migration converting Sequelize.UUID primary key field to integer autoincrement in MYSQLSequelize 迁移将 Sequelize.UUID 主键字段转换为 MYSQL 中的整数自增
【发布时间】:2018-10-20 17:48:18
【问题描述】:

我正在使用 sequelize CLI 来生成和运行数据库迁移。我遇到的问题是设置为数据类型 Sequelize.UUID 的 id 字段在 mysql 中显示为自动增量整数。这是我的用户模型和迁移:

用户模型

    'use strict';
    module.exports = (sequelize, DataTypes) => {
      const User = sequelize.define('User', {
        UserName: {type:DataTypes.STRING,unique:true,allowNull:false},
        FirstName:{type: DataTypes.STRING,allowNull:true},
        LastName: {type:DataTypes.STRING,allowNull:true},
        Email: {type:DataTypes.STRING,allowNull:false,unique:true,validate: { isEmail: {msg: "Invalid Email"} }},
        Password: {type:DataTypes.STRING,allowNull:false},
        Avatar: {type:DataTypes.STRING,allowNull:true},

      }, {});
      User.associate = function(models) {

        User.hasMany(models.RoleUser,
            {
                foreignKey:'UserId',
                as:'userroles',
                sourceKey:'id'
            }),

        User.belongsTo(models.Country,
            {
                foreignKey:'CountryId',
                targetKey:'id'
            }),

            User.belongsToMany(models.Role,
                {
            through: 'RoleUser',
            foreignkey:'UserId'

        })


     };
      return User;
    };

**User Migration file:**

'use strict';
const uuidv4 = require('uuid/v4');

module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Users', {
      id: {
        allowNull: false,
        primaryKey: true,
        type: Sequelize.UUID
        defaultValue:uuidv4()
      },
      UserName: {
        type: Sequelize.STRING
      },
      FirstName: {
        type: Sequelize.STRING
      },
      LastName: {
        type: Sequelize.STRING
      },
      Email: {
        type: Sequelize.STRING
      },
      Password: {
        type: Sequelize.STRING
      },
      Avatar: {
        type: Sequelize.STRING
      },

      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Users');
  }
};

迁移后,他的字段在 MYSQL 中转换为 INT AUTOINCREMENT:

id: {
            allowNull: false,
            primaryKey: true,
            type: Sequelize.UUID
            defaultValue:uuidv4()
          },

关于为什么会发生这种情况的任何指针?请协助。甚至关联似乎根本没有形成,因为外键是 Sequelize.UUID 类型的

【问题讨论】:

    标签: migration sequelize.js uuid


    【解决方案1】:

    我通过在主键属性设置为 true 的模型上添加 id 字段来解决问题。

     id: {
    type:DataTypes.UUID,
    allowNull:false,
    unique:true,
    primaryKey:true
    },
    

    如果模型没有主键设置为true的字段,sequelize会自动生成类型为INTEGER AUTOINCREAMENT的id字段。

    【讨论】:

      猜你喜欢
      • 2016-07-01
      • 2016-10-08
      • 2016-11-15
      • 2022-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-28
      相关资源
      最近更新 更多