Sails version: v1.0.0-36
Node version: any
NPM version: any
Operating system: any
Sails v1.x.x requires that id (or whatever the primary key is) is either set to required or autoIncrement. This makes it difficult (impossible?) to use a uuid (uuid.v4()) as the primary key.
In v0.12.x, either of these would work:
autoPK: false,
attributes: {
id: {
type: 'string',
primaryKey: true,
defaultsTo: function() {
return uuid.v4();
}
}
}
or
autoPK: false,
attributes: {
id: {
type: 'string',
primaryKey: true
}
},
beforeCreate: function(values, cb) {
values.id = uuid.v4();
cb();
}
In v1.x.x, neither of the above work. Is there another way to use a uuid as the primary key?
@fenichelar Thanks for posting, we'll take a look as soon as possible.
For help with questions about Sails, click here. If you’re interested in hiring @sailsbot and her minions in Austin, click here.
@fenichelar Interesting question -- is there an issue with setting the id directly in your .create() statement or blueprint POST?
@sgress454 Not if I was using .create() lol. I am using the automatic rest blueprints, my favorite part of Sails! 馃槃 What do you mean in the blueprint POST?
@fenichelar When you use the automatic blueprint, you're still setting some attributes, right? You should be able to set id as one of those. You'll just need to generate your UUID on the front end.
Ahh I see, I think UUIDs on the front end could be very dangerous. I am using UUIDs for uniqueness across tables. Malicious users could try to exploit this.
I ended up finding a solution. I used a policy to set the id. It is just a little weird to do it that way. It would be great if the Sails 1.x.x restrictions preventing defaults to on a primary key was removed as defaults to can be a function.
@fenichelar glad you found a solution, and we'll look into this as a feature. Re: security issue, without a policy like the one you're describing, the app would have that security issue regardless -- a malicious user could still just POST to the endpoint and set whatever id they wanted. Policies are there to protect your endpoints from this kind of silliness!
How did you get this question? Because I am creating a system where I need to generate the UUID inside the WebService, and I fall into the same context of the @fenichelar
Hey, Is anyone have solution to set uuid() as Primary key without using required: true in model attributes. I am facing issues in setting uuid as the primary key, it through me a error
primary key
idmust have eitherrequiredorautoIncrementset
I have required: true set for the id. I have a policy that applies to all create actions:
const uuid = require('uuid');
const actionUtil = require('sails-ember-rest/templates/util/actionUtil');
module.exports = (req, res, next) => {
const model = actionUtil.parseModel(req);
if (!model) {
return res.badRequest();
}
const identity = _.camelCase(model.globalId);
if (!identity) {
return res.badRequest();
}
if (!req.param(identity)) {
return res.badRequest();
}
req.param(identity).id = uuid.v4();
return next();
};
Hi @umarfarook882 , @fenichelar ,
I'm sorry for the late comment, I just want to show how to setup model to use UUID with 'sails-postgresql'.
on your postgree database, you need to create new extension :
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
on config/model.js :
migrate: 'drop',
attributes: {
id: { type: 'string', columnType: 'UUID DEFAULT uuid_generate_v4()', autoIncrement: true},
}
_you should setting migrate to 'drop' or 'safe'_ if you setting to 'alter' you will get an error message.
You can find that ways on sails-postgresql code here
https://github.com/balderdashy/sails-postgresql/blob/881275fc999e680f517eb4dccbc99a7bb3dbe1ce/helpers/private/schema/build-schema.js#L51
Hopes it can helps anyone who want to use UUID on their primary key. Thanks.
Does the Sails project have more than 5 Years and there is still no functionality to generate UUID for IDs?
Somewhere there must be some documentation for such a thing.
Thanks @toni-ismail . Currently i am generating the UUID on controller side and using that as primary key in model with the following config. it's seem working fine for me. Anyway i will give a try with your idea.
primaryKey: 'uuid',
attributes: {
uuid: { type: 'string',required: true,isUUID: true}
}
Does the Sails project have more than 5 Years and there is still no functionality to generate UUID for IDs?
Somewhere there must be some documentation for such a thing.
That's true. Even if the trick is easy it requires attention:
[...]
primaryKey: 'id',
dontUseObjectIds: true,
[...]
id: {
type: 'string',
isUUID: true,
columnName: '_id',
unique: true,
},
hi, is there an update/solution to make blueprint model attribute's id to default to uuid v4?
What i want to do is to overwrite all models in config/models.js to have id assigned a varchar(36) uuid v4
EDIT: I used sails parseBlueprintOptions to set id = uuid for every create action
@deeyonn Please can you share with me the code of how you have done this in the blueprint?