Javascript sails在创建没有id的postgresql新用户时返回错误
我想使用sails在PostgreSQL数据库中创建一个新用户。在我的PostgreSQL数据库中,我的id字段有一个序列。在我的model.js中,我有:Javascript sails在创建没有id的postgresql新用户时返回错误,javascript,database,postgresql,orm,sails.js,Javascript,Database,Postgresql,Orm,Sails.js,我想使用sails在PostgreSQL数据库中创建一个新用户。在我的PostgreSQL数据库中,我的id字段有一个序列。在我的model.js中,我有: attributes: { id: { type: 'number', required: true, unique: true, autoIncrement: true, }, first_name: { type: 'string', required: true }, last_name: { type:
attributes: {
id: {
type: 'number',
required: true,
unique: true,
autoIncrement: true,
},
first_name: {
type: 'string',
required: true
},
last_name: {
type: 'string',
required: true
},
company: {
type: 'string',
allowNull: true
},
url: {
type: 'string',
allowNull: true
},
email: {
type: 'string',
required: true,
unique: true,
isEmail: true
},
password: {
type: 'string',
required: true
},}
我要创建的对象是:
{ first_name: 'g',
last_name: 'f',
company: 'f',
url: 'u',
email: 't@h.co',
password: 'Aa123456@' }
但是当我试图运行我的项目时,我得到了一个错误
缺少必需属性的值
id。应为一个数字,但改为:未定义
。现在该怎么办?由于id
是必填字段,ORM生命周期中的验证失败,因为它期望id
出现
您可以从模型中删除id
属性,Postgres将自动处理自动递增字段,或者您必须在验证前添加钩子/函数,在验证前将数据显式分配给id
字段
验证前的示例钩子:以及sails如何使用它?我不知道它是怎么工作的!我使用了你的功能,但我甚至在控制台中也看不到任何东西