geo*_*rge 12 node.js express sails.js waterline
以下代码表示Sails.js v0.9.4中的帐户模型.
module.exports = {
attributes: {
email: {
type: 'email',
unique: true,
required: true
},
password:{
type: 'string',
minLength: 6,
maxLength: 15,
required:true
}
}
};
Run Code Online (Sandbox Code Playgroud)
当我通过邮递员发送两个POSTS和一个PUT请求到localhost:8080/account时,电子邮件的唯一属性失败.具体来说,我从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
Run Code Online (Sandbox Code Playgroud)
最后一个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
}
]
Run Code Online (Sandbox Code Playgroud)
这会发生吗?
*对于那些不知道的人,Waterline默认生成一个id,它会在每次插入时自动递增.
小智 10
这是因为您的架构未在磁盘数据库中更新(".tmp/disk.db").
您需要关闭风帆,丢弃数据库并重新启动风帆.数据库将使用您的良好架构进行重建.
注意:数据也会下降!
如果要保留数据,只需更新".tmp/disk.db"的架构部分即可.
我通过sails.js保持数据和重建架构的工作:
您必须在模式(文件".tmp/disk.db" - >"schema"部分)中为唯一字段设置此项:
"xxx": {
"type": "string",
"unique": true
},
Run Code Online (Sandbox Code Playgroud)
我希望这对你有帮助.
我遇到了同样的问题.要解决此问题,您必须避免使用"磁盘"ORM适配器.由于某种原因,它似乎不支持唯一性检查.
其他适配器(如mongo和mysql)应支持唯一性检查,因此这不应该是开发之外的问题.
对于开发过程,将默认适配器config/adapters.js
从"disk"更改为"memory".应该是这样的:
module.exports.adapters = {
// If you leave the adapter config unspecified
// in a model definition, 'default' will be used.
'default': 'memory',
// In-memory adapter for DEVELOPMENT ONLY
memory: {
module: 'sails-memory'
},
...
};
Run Code Online (Sandbox Code Playgroud)