So, I'm on my merry way to implementing oAuth2 on my network, and I'm having serious problems of updating the code for the ['client_id','user_id'], I've set it up in my Model too.
My aim is if it exists in the DB, update it, otherwise insert, my hack for a upsert of sorts. But this way I've gotten a strange error that says I haven't specified a where or even an idAttribute:
Error updating auth code: Error: A model cannot be updated without a "where" clause or an idAttribute.
Looking over this whole mess I've made, makes me want to go sleep after working at this for over 20 hours now. This server is just about done. Missing this one component, and I'm set.
"use strict";
var Models = require("bookshelf-model-loader");
var AuthCode = Models.Bookshelf.Model.extend({
tableName: "oauth2_auth_codes",
idAttribute: ["client_id","user_id"],
client: function () { return this.belongsTo(Models.Client); },
user: function() { return this.belongsTo(Models.User); }
});
module.exports = {
AuthCode: Models.Bookshelf.model("AuthCode", AuthCode)
};
server.grant(oauth2orize.grant.code(function (client, redirectURI, user, ares, done) {
var codeLookup = {
code: utils.uid(16),
client_id: client.id,
redirect_uri: redirectURI,
user_id: user.user_id
};
new Models.AuthCode({user_id: user.user_id, client_id: client.id})
.fetch({require: true})
.then(function (auth) {
//Found, update
new Models.AuthCode({user_id: user.user_id, client_id: client.id})
.save(codeLookup, {method: "update"})
.then(function (authCode) {
done(null, authCode.get("code"));
})
.catch(function (err) {
console.log("Error updating auth code:", err.toString());
done(err);
});
})
.catch(Models.AuthCode.NotFoundError, function () {
// Not found, insert
new Models.AuthCode()
.save(codeLookup)
.then(function (authCode) {
done(null, authCode.get("code"));
})
.catch(function (err) {
console.log("Error inserting auth code:", err.toString());
done(err);
});
})
.catch(function (err) {
console.log("Failed to find auth code: ", err);
done(err);
});
}));
Hi @cuteboi. Sorry for the slow response. The documentation is totally misleading here. Since before I've been involved with Bookshelf it does not fully support composite primary keys. AFAIK it is supported by isNew and that's about it. I'm not sure if there was ever full support.
However! This is definitely something I'd like to see happening. Adding save() support for models with composite keys is not difficult. Problem starts here.
I'm marking this as a bug. Two possible PR's here:
Model#save(). This might require a few helper functions.Thanks for the update, I'll test a modification from that location you stated, and provide a PR for point number 2.
Thanks for pointing in the right direction. I hate digging through source I didn't code for something documentation says otherwise. Why search if it may be obscure, but in this case it wasn't so obscure.
Hi, has this issue been resolved in any way? I'm interested in composite primary keys.
Could this be handled with a parse / format hack?
I'm currently using a null value which doesn't throw errors when creating instances of a model with multiple column id's. My model definition:
Bookshelf.Model.extend({
tableName: 'table1_table2',
idAttribute: null
})
Most helpful comment
I'm currently using a
nullvalue which doesn't throw errors when creating instances of a model with multiple column id's. My model definition: