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
updates using UNIQUE (but not PRIMARY) key #51
Comments
I may need to improve the documentation here, but when dropping into the query layer with the intention of continuing to use the current model (or collection) as the context for querying, you'll want to pass either a function or conditions to the query method... so what you'll want is either: var entity = models.Entity.forge(); // for brevity
entity.query('where', 'uuid', '=', req.params.uuid).save(attributes).then(function(model) {...
// or
entity.query({where: ['uuid', '=', req.params.uuid]}).save(attributes).then(function(model) {...
// or (as of 0.2.5)
entity.query(function(qb) {
qb.where('uuid', '=', req.params.uuid);
}).save(attributes).then(... This way when you call This can also be useful for debugging, as adding: entity.query('debug').save(... Would effectively be the same as calling The documentation section for Also, just incase you'd find this useful, if you set the Let me know if that helps with the questions you have! |
Hi Tim, Thanks so much for the follow-up. I just tried out models.Entity
.forge()
.query({where: ['uuid', '=', req.params.uuid]})
.save(attributes, {method: 'update'})
.then(function(model) {
// success
}, function(err) {
// error
}); But by forcing the
As a workaround, we could first If related, I also tried passing models.Entity
.forge()
.query(function(qb) {
console.log('before');
qb.where('uuid', '=', '110ec58a-a0f2-4ac4-8393-c866d813b8d1');
console.log('after');
})
.save(attributes, {method: 'update'})
.then(function(model) {
// success
}, function(err) {
// error
}); Much obliged! |
Should use call rather than apply, test included
I agree it's ideal to avoid the extra select there - in the upcoming In the meantime, I changed a bit of the logic dealing with the timestamp to check the Also, thanks a lot for pointing out the bug in the query function, I forgot to add a test - should've changed it from |
Thanks very much, both changes seem to be working nicely now! |
Hi all,
We currently have a MySQL table whose rows include, e.g.:
id
uuid
name
The
uuid
is a UUIDv4 that exists so that we have a unique, essentially unguessable identifier for an entity that can be used in URLs, without exposing the underlying primary key (id
). We haven't been sure, though, of the best way tofetch
andupdate
models using that uniqueuuid
, though. AlteringidAttribute
hasn't felt ideal, because thenuuid
becomes our primary key, which isn't what we want for joins. We're currently usingwhich works. But we ran into a side effect whereby, because we're dropping down to the query layer with the above, a hook we've defined in our model's
initialize
method isn't invoked:Are we better off implementing one or both of these in some other way perhaps? We did notice that the documentation for
Bookshelf.Sync.update
alludes to "a custom query", but we weren't sure if that might help us with both goals?Many thanks!
The text was updated successfully, but these errors were encountered: