Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Request: GUIDs as Primary Keys #6369

Closed
ghballiet opened this issue Aug 8, 2013 · 5 comments
Closed

Feature Request: GUIDs as Primary Keys #6369

ghballiet opened this issue Aug 8, 2013 · 5 comments

Comments

@ghballiet
Copy link

(migrated from my original post in Sails)

I prefer to use GUIDs instead of integer primary keys in my apps, and I'd like to be able to do this within Sails. This is definitely lower priority than, say, associations, but I don't believe I'm the only one who uses this, either (see: http://www.codinghorror.com/blog/2007/03/primary-keys-ids-versus-guids.html).

@glenselle
Copy link
Contributor

It looks like you can specify a primary key, and manually create GUIDs from your application and set those to the respective column. It seems to work just fine. Here's what I did:

I used the node-uuid module (https://github.com/broofa/node-uuid):

var uuid = require('node-uuid');

Then in the model where I wanted to use GUIDs, I just set primaryKey to true and set the default to generate a new GUID.

id: {
    type: 'STRING',
    primaryKey: true,
    defaultsTo: uuid.v4()
}

Also, I set autoPK: false within my model to prevent Waterline from automatically creating a PK column.

@CWyrtzen
Copy link

CWyrtzen commented May 8, 2014

We're closing this to keep things tidy. It has been tracked as a feature request. Thanks for sharing your ideas and feedback. And, as always, you can reopen an issue at any time if you need more support.

@CWyrtzen CWyrtzen closed this as completed May 8, 2014
@CWyrtzen
Copy link

@mikebiglan
Copy link

One important note for others that grab @glenselle's helpful snippet above. Make sure not to call the uuid.v4 function or else it will do it once at instantiation and use the same GUID for each new record.

id: {
    type: 'STRING',
    primaryKey: true,
    defaultsTo: uuid.v4
}

@gaurav21r
Copy link

@glenselle @mikebiglan +1 👍

@johnabrams7 johnabrams7 transferred this issue from balderdashy/waterline May 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

5 participants