How do I setup UUID / GUID for Model primary keys ?

899 views
Skip to first unread message

Kevin Baker

unread,
Jul 7, 2014, 1:45:33 PM7/7/14
to sai...@googlegroups.com
How would I go about setting up a custom schema in a Sails Model to allow for UUID's for the "id" primary key?

I would love to do something like this.

```
attributes: {

  id: {
    type: 'uuid',
    required: true
  }
}
```

Or just `id: 'string' with a beforeCreate generateUUID filter at the end.

Would this work? Is there a best practice?

Thanks

Kevin Baker

unread,
Jul 7, 2014, 1:50:17 PM7/7/14
to sai...@googlegroups.com
BTW: did see one other post on this, but there didn't seem to be a resolution. Thanks for your help.

Mike Hostetler

unread,
Jul 7, 2014, 1:51:35 PM7/7/14
to Kevin Baker, sai...@googlegroups.com
I don't know that Waterline supports this so I'd recommend the second approach, use an id: with type 'string' and assign a UUID with a beforeCreate hook.

----------------
Mike Hostetler 
----------------
BLOG | http://mike-hostetler.com
TWITTER | http://twitter.com/mikehostetler

Kevin Baker

unread,
Jul 7, 2014, 2:01:26 PM7/7/14
to Mike Hostetler, sai...@googlegroups.com
Great, I just found the StackOverflow post: http://stackoverflow.com/questions/21386573/how-can-i-write-sails-function-on-to-use-in-controller. That has details on how to do exactly what you describe.

I found this helpful too in the Waterline docs: https://github.com/balderdashy/waterline-docs/blob/master/models.md (search for uuid)

Thanks for the guidance!


Mike Hostetler

unread,
Jul 7, 2014, 2:09:07 PM7/7/14
to Kevin Baker, sai...@googlegroups.com



----------------
Mike Hostetler 
----------------
BLOG | http://mike-hostetler.com
TWITTER | http://twitter.com/mikehostetler


Kevin Baker

unread,
Jul 8, 2014, 12:00:33 PM7/8/14
to Mike Hostetler, sai...@googlegroups.com
Nice !
Reply all
Reply to author
Forward
0 new messages