`attach` with additional attributes
See original GitHub issueWhen I call attach
I generally want to be able to set other attributes on the M2M relation.
A common example might be a timestamp.
Something like collection.belongsToMany(db.Models.Link).attach(link, {archived: false})
I’d also like to be able to set defaults and methods for the M2M relation.
class CollectionsLinks extends Bookshelf.Model
defaults:
archived: false
I found the following which seems to indicate you can’t pass in a model and additional params - I’d have to create a hash myself.
// If the item is an object, it's either a model
// that we're looking to attach to this model, or
// a hash of attributes to set in the relation.
if (_.isObject(item)) {
if (item instanceof Model) {
data[relatedData.foreignKey] = item.id;
} else {
_.extend(data, item);
}
} else if (item) {
data[relatedData.foreignKey] = item;
}
Also the comments for belongsToMany
say:
The joinTableName may be replaced with another object, will serve as the joining model.
I expected to be able to pass a Bookshelf.Model
, whose defaults
would be set on attach, as well as timestamps, etc.
Examples of using many to many relations with additional attrs on the join table would be helpful.
Issue Analytics
- State:
- Created 10 years ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
I ended up here with the same question after searching 😉, I then found the following hints, which I thought it would be helpful to post here:
Hi @ErisDS. Thanks for pointing this out. I’ll try to update the docs soon.