Skip to content

Instantly share code, notes, and snippets.

@mikermcneil
Last active December 6, 2016 02:16
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
How do I change a model's primary key attribute in Sails/Waterline?

How do I change a model's primary key attribute?

There are plenty of good reasons to change a primary key in a database. In Sails/Waterline, you can customize your primary key's physical layer representation using columnName (whether you're in Mongo, MySQL, etc). This is the recommended approach, since it keeps your application code safely insulated from some of these sorts of changes you might need to make in the underlying database.

No, I mean like the attribute name.

As far as changing the name of the primary key attribute itself? That's a stickier subject. There are some completely reasonable use cases for this, but it is my experience that (1) there is always another way and that (2) the pros never outweigh the cons.

You can do it!

But... please, reconsider.

For better or worse, the name of the default primary key attribute in Sails/Waterline is "id". Changing that convention will make your app non-standard. This is not necessarily bad, in and of itself, but it does make your code harder to maintain over the long term. For example, most of the online documentation, tutorials, and examples for Sails/Node.js use "id" as the primary key. By deparating from that in your app, you're making all of those resources a little less relevant for yourself and other developers. On top of that, if you ever want to change the name of your primary key back again, you'll be stuck doing an error-prone find/replace across many different files in your back-end code base, and then manually verifying changes by hand.

My point is that you're probably better off sticking to the default. Just my 2¢.

-Mike

Dec 5, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment