以下代码表示 Sails.js v0.9.4 中的帐户模型。
module.exports = {
attributes: {
email: {
type: 'email',
unique: true,
required: true
},
password:{
type: 'string',
minLength: 6,
maxLength: 15,
required:true
}
}
};
当我通过 Postman 向 localhost:8080/account 发送两个 POST 和一个 PUT 请求时,电子邮件的唯一属性失败。 具体来说,我从 Postman 发送以下 HTTP 请求:
POST http://localhost:8080/account?email=foo@gmail.com&password=123456
POST http://localhost:8080/account?email=bar@gmail.com&password=123456
PUT http://localhost:8080/account?id=1&email=bar@gmail.com
GET http://localhost:8080/account
最后一个 GET 请求显示我:
[
{
"email": "bar@gmail.com",
"password": "123456",
"createdAt": "2013-09-30T18:33:00.415Z",
"updatedAt": "2013-09-30T18:34:35.349Z",
"id": 1
},
{
"email": "bar@gmail.com",
"password": "123456",
"createdAt": "2013-09-30T18:33:44.402Z",
"updatedAt": "2013-09-30T18:33:44.402Z",
"id": 2
}
]
这应该发生吗?
*对于那些不知道的人,Waterline 默认会生成一个 id,它会在每次插入时自动递增。
最佳答案
这是因为您的架构未在您的磁盘数据库(“.tmp/disk.db”)中更新。
您需要关闭sails,删除您的数据库并重新启动sails。 将使用您的良好架构重建数据库。
注意:数据也会掉!
如果您想保留数据,只需更新“.tmp/disk.db”的架构部分即可。
我为保存数据和通过sails.js 重建架构所做的工作:
- 复制“.tmp/disk.db”
- 清理“.tmp/disk.db”
- 关闭sails.js
- 启动sails.js -> 数据库为空,模式已更新
- 复制旧的“计数器”部分
- 复制旧的“数据”部分
对于唯一字段,您必须在架构(文件“.tmp/disk.db”->“架构”部分)中有这个:
"xxx": {
"type": "string",
"unique": true
},
希望对你有所帮助。
关于node.js - Sails.js 中的唯一属性失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19101531/