一段时间以来,我一直在尝试将Users表的id (主键)设置为UUID。但是,我一直收到这样的错误:当我尝试播种字段'id‘时,它没有默认值。
这是我到目前为止在我的用户模型中所做的:
'use strict';
const {
Model
} = require('sequelize');
module.exports = (sequelize, DataTypes) => {
class Users extends Model {};
Users.init({
id: {
type: DataTypes.UUID,
defaultValue: DataTypes.UUIDV4,
allowNull: false,
primaryKey: true
},
user_name: {
type: DataTypes.STRING,
allowNull: false,
validate: {
notNull: {
msg: 'Please add a name',
},
},
},
{
sequelize,
modelName: 'Users',
});
return Users;同样,这是我在用户迁移文件中的内容:
'use strict';
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.createTable('Admins', {
id: {
type: Sequelize.UUID,
defaultValue: Sequelize.UUIDV4,
allowNull: false,
primaryKey: true
},
user_name: {
type: Sequelize.STRING
},
createdAt: {
allowNull: false,
type: Sequelize.DATE
},
updatedAt: {
allowNull: false,
type: Sequelize.DATE
}
});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.dropTable('Users');
}
};我是一个很新的续集,所以我想要一些关于哪里出了问题的指导!
发布于 2021-06-10 01:06:46
所以,我想我意识到了问题所在:
由于某些原因,使用种子文件播种它不会自动生成我认为会自动生成的字段,因此我必须手动将其放入。
'use strict';
const { v4: uuidv4 } = require('uuid');
module.exports = {
up: async (queryInterface, Sequelize) => {
await queryInterface.bulkInsert('Users', [{
id: uuidv4(),
user_name: 'John Doe',
"createdAt": new Date(),
"updatedAt": new Date()
}], {});
},
down: async (queryInterface, Sequelize) => {
await queryInterface.bulkDelete('Users', null, {});
}
};最初,我一直试图在没有id: uuidv4()和新日期()的情况下进行种子(运行命令npx sequelize cli db: seed :all),这就是它不起作用的原因。
https://stackoverflow.com/questions/67892769
复制相似问题