Sails-redisを利用した際に`Details:  AdapterError: Record does not satisfy unique constraints`で失敗する


sailsを利用していて、ある日テスト用の`salis-disk`から`sails-redis`に切り替えた際に
さくっとエラーで動かなくなったので、メモしておきます。

## 発生条件
具体的な条件としては、create対象のModelが1件でも存在している状態で、
Model#createで渡すObjectに`id`プロパティが`undefined`だろうと`null`だろうと`""`だろうと
存在していた場合に失敗します。

エラー内容は以下の通りです。
> Error (E_UNKNOWN) :: Encountered an unexpected error
AdapterError: Record does not satisfy unique constraints
  at ~/sails-test/node_modules/sails-redis/lib/database/index.js:777:41
  at ~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:1170:13
  at try_callback (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:592:9)
  at RedisClient.return_reply (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:685:13)
  at ReplyParser.<anonymous> (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:321:14)
  at ReplyParser.EventEmitter.emit (events.js:95:17)
  at ReplyParser.send_reply (~/sails-test/node_modules/sails-redis/node_modules/redis/lib/parser/javascript.js:300:10)
  at ReplyParser.execute (~/sails-test/node_modules/sails-redis/node_modules/redis/lib/parser/javascript.js:211:22)
  at RedisClient.on_data (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:547:27)
  at Socket.<anonymous> (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:102:14)
  at Socket.EventEmitter.emit (events.js:95:17)
  at Socket.<anonymous> (_stream_readable.js:746:14)
  at Socket.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at Socket.Readable.push (_stream_readable.js:127:10)
  at TCP.onread (net.js:528:21)
>
Details:  AdapterError: Record does not satisfy unique constraints

## 回避策
事前にプロパティをdeleteする事で回避可能です。

```javascript
var user = {id: "", title: "test"};
delete user.id
User.create(user).exec(console.log);
```

## 検証ログ
最初発生原因が分からず色々検証した結果です。
`sails console`で起動し、redis-cliのMONITORで実際のクエリを監視しています。

```bash
# 最初のModel作成
sails> User.create({id: "", title:"hoge"}).exec(console.log)
undefined
sails> null { id: 1,
  title: 'hoge',
  createdAt: Wed Dec 31 2014 22:34:35 GMT+0900 (JST),
  updatedAt: Wed Dec 31 2014 22:34:35 GMT+0900 (JST) }
# 2つ目のModel作成(id:""を設定。undefinedでも同様に発生)
sails> User.create({id: "", title:"hoge"}).exec(console.log)
undefined
sails> Error (E_UNKNOWN) :: Encountered an unexpected error
AdapterError: Record does not satisfy unique constraints
  at ~/sails-test/node_modules/sails-redis/lib/database/index.js:777:41
  at ~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:1170:13
  at try_callback (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:592:9)
  at RedisClient.return_reply (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:685:13)
  at ReplyParser.<anonymous> (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:321:14)
  at ReplyParser.EventEmitter.emit (events.js:95:17)
  at ReplyParser.send_reply (~/sails-test/node_modules/sails-redis/node_modules/redis/lib/parser/javascript.js:300:10)
  at ReplyParser.execute (~/sails-test/node_modules/sails-redis/node_modules/redis/lib/parser/javascript.js:211:22)
  at RedisClient.on_data (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:547:27)
  at Socket.<anonymous> (~/sails-test/node_modules/sails-redis/node_modules/redis/index.js:102:14)
  at Socket.EventEmitter.emit (events.js:95:17)
  at Socket.<anonymous> (_stream_readable.js:746:14)
  at Socket.EventEmitter.emit (events.js:92:17)
  at emitReadable_ (_stream_readable.js:408:10)
  at emitReadable (_stream_readable.js:404:5)
  at readableAddChunk (_stream_readable.js:165:9)
  at Socket.Readable.push (_stream_readable.js:127:10)
  at TCP.onread (net.js:528:21)

Details:  AdapterError: Record does not satisfy unique constraints
# id:""なしで実行
sails> User.create({title:"hoge"}).exec(console.log)
undefined
sails> null { title: 'hoge',
  createdAt: Wed Dec 31 2014 22:37:29 GMT+0900 (JST),
  updatedAt: Wed Dec 31 2014 22:37:29 GMT+0900 (JST),
  id: 2 }
```

```bash
# User.create({id: "", title:"hoge"}).exec(console.log)
1420032875.597175 [0 127.0.0.1:60697] "MULTI"
1420032875.597206 [0 127.0.0.1:60697] "sismember" "waterline:user:_indicies:id" "NaN"
1420032875.597223 [0 127.0.0.1:60697] "EXEC"
1420032875.600085 [0 127.0.0.1:60697] "sismember" "waterline:user:_indicies:id" "NaN"
1420032875.600531 [0 127.0.0.1:60697] "sadd" "waterline:user:_indicies:id" "NaN"
1420032875.600971 [0 127.0.0.1:60697] "incr" "waterline:user:_sequences:id"
1420032875.601456 [0 127.0.0.1:60697] "set" "waterline:user:id:1" "{\"id\":1,\"title\":\"hoge\",\"createdAt\":\"2014-12-31T13:34:35.596Z\",\"updatedAt\":\"2014-12-31T13:34:35.596Z\"}"
1420032875.601777 [0 127.0.0.1:60697] "get" "waterline:user:id:1"
1420032875.605162 [0 127.0.0.1:60697] "sadd" "waterline:user:id" "waterline:user:id:1"
1420032875.605199 [0 127.0.0.1:60697] "sismember" "waterline:user:_indicies:id" "1"
1420032875.605963 [0 127.0.0.1:60697] "sadd" "waterline:user:_indicies:id" "1"


# User.create({id: "", title:"hoge"}).exec(console.log)
1420032949.726551 [0 127.0.0.1:60697] "MULTI"
1420032949.726584 [0 127.0.0.1:60697] "sismember" "waterline:user:_indicies:id" "NaN"
1420032949.726605 [0 127.0.0.1:60697] "EXEC"


# User.create({title:"hoge"}).exec(console.log)
1420033049.867159 [0 127.0.0.1:60697] "MULTI"
1420033049.867187 [0 127.0.0.1:60697] "EXEC"
1420033049.868439 [0 127.0.0.1:60697] "incr" "waterline:user:_sequences:id"
1420033049.869021 [0 127.0.0.1:60697] "set" "waterline:user:id:2" "{\"title\":\"hoge\",\"createdAt\":\"2014-12-31T13:37:29.866Z\",\"updatedAt\":\"2014-12-31T13:37:29.866Z\",\"id\":2}"
1420033049.869291 [0 127.0.0.1:60697] "get" "waterline:user:id:2"
1420033049.869765 [0 127.0.0.1:60697] "sadd" "waterline:user:id" "waterline:user:id:2"
1420033049.869793 [0 127.0.0.1:60697] "sismember" "waterline:user:_indicies:id" "2"
1420033049.870213 [0 127.0.0.1:60697] "sadd" "waterline:user:_indicies:id" "2"
```

## オチ
そういえばissue登録されているかなぁと思ってgithub検索してみたら、見事に見つけましたorz..。
まだcloseされていませんが...。

* [AdapterError: Record does not satisfy unique constraints](https://github.com/balderdashy/sails-redis/issues/39)