gpt4 book ai didi

mysql - 使用 mySQL : Unable to set default value 续写 UUID

转载 作者:行者123 更新时间:2023-12-03 22:29:59 26 4
gpt4 key购买 nike

我一直在尝试将我的用户表的 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');
}
};
我对 Sequelize 还很陌生,所以希望能得到一些关于出了什么问题的指导!

最佳答案

所以,我想我意识到了这个问题:
出于某种原因,使用播种机文件进行播种不会自动生成我认为会自动生成的字段,因此我必须手动将其放入。

'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() 和 new Date() 的情况下播种(运行命令 npx sequelize-cli db:seed:all),这就是它不起作用的原因。

关于mysql - 使用 mySQL : Unable to set default value 续写 UUID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67892769/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com