Sails.js中的唯一属性失败 [英] Unique property fails in Sails.js
问题描述
以下代码表示Sails.js v0.9.4中的帐户模型。
The following code represents an Account Model in Sails.js v0.9.4 .
module.exports = {
attributes: {
email: {
type: 'email',
unique: true,
required: true
},
password:{
type: 'string',
minLength: 6,
maxLength: 15,
required:true
}
}
};
google.com/webstore/detail/postman-rest-client/fdmmgilgnpjigdojojpjoooidkmcomcm?hl=enrel =noreferrer> Postman 到本地主机:8080 /帐户,电子邮件的唯一属性失败。
具体来说,我从Postman发送以下HTTP请求:
When I send two POSTS and a PUT request via Postman to localhost:8080/account, the unique property of the email fails. Specifically, I send the following HTTP requests from Postman:
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请求显示:
The last GET request shows me:
[
{
"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。
Should this happen?
*For those who don't know, Waterline generates by default an id which automatically increments in every insertion.
推荐答案
p>这是因为您的磁盘数据库(.tmp / disk.db)中的模式未更新。
This is because your schema is not updated in your disk database (".tmp/disk.db").
您需要关闭sails,删除数据库和重启风帆
数据库将重建您的良好架构。
You need to shutdown sails, drop your DB and restart sails. The DB will be reconstruct with your good schema.
注意:数据也将下降!
如果你想保留你的数据,你可以更新.tmp / disk.db的架构部分。
If you want keep your data, you can just update the schema part of ".tmp/disk.db".
我在做什么来保存数据和通过sails.js重建模式:
What I have doing to keep data and rebuild schema by sails.js :
- 复制.tmp / disk.db
- .tmp / disk.db
- shutdown sails.js
- start sails.js
- >数据库为空,模式更新 - 复制旧的计数器部分
- 复制旧的数据部分
- copy ".tmp/disk.db"
- clean ".tmp/disk.db"
- shutdown sails.js
- start sails.js -> the database is empty and the schema is updated
- copy old "counters" part
- copy old "data" part
您必须在您的模式(文件.tmp / disk.db - >模式部分)中具有唯一字段:
You must have this in your schema (file ".tmp/disk.db" -> "schema" part) for the unique field :
"xxx": {
"type": "string",
"unique": true
},
希望这有助于您。
这篇关于Sails.js中的唯一属性失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!