Sequelize 迁移将 Sequelize.UUID 主键字段转换为 MYSQL 中的整数自动增量
Sequelize migration converting Sequelize.UUID primary key field to integer autoincrement in MYSQL
我正在使用 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');
}
};
迁移后,HIS 字段在 MYSQL 中转换为 INT 自增:
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID
defaultValue:uuidv4()
},
关于为什么会发生这种情况的任何指示?请协助。由于外键是 Sequelize.UUID
类型,甚至关联似乎根本没有形成
我通过在主键 属性 设置为 true 的模型上添加 id 字段解决了这个问题。
id: {
type:DataTypes.UUID,
allowNull:false,
unique:true,
primaryKey:true
},
如果模型没有主键设置为true的字段,sequelize会自动生成INTEGER AUTOINCREAMENT类型的id字段。
我正在使用 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');
}
};
迁移后,HIS 字段在 MYSQL 中转换为 INT 自增:
id: {
allowNull: false,
primaryKey: true,
type: Sequelize.UUID
defaultValue:uuidv4()
},
关于为什么会发生这种情况的任何指示?请协助。由于外键是 Sequelize.UUID
类型,甚至关联似乎根本没有形成我通过在主键 属性 设置为 true 的模型上添加 id 字段解决了这个问题。
id: {
type:DataTypes.UUID,
allowNull:false,
unique:true,
primaryKey:true
},
如果模型没有主键设置为true的字段,sequelize会自动生成INTEGER AUTOINCREAMENT类型的id字段。