question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`attach` with additional attributes

See original GitHub issue

When 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:closed
  • Created 10 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
ErisDScommented, Jun 29, 2015

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:

0reactions
rhys-vdwcommented, Jun 30, 2015

Hi @ErisDS. Thanks for pointing this out. I’ll try to update the docs soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Attach extra attributes to Laravel pivot table - Stack Overflow
Just one array will do: $invoice->productversion()->attach($productversionid, [ 'product_id' => $productid, 'quantity' => $qty, ...
Read more >
To Create and Attach an Attribute Definition | AutoCAD 2020
Click Home tabBlock panelDefine Attributes. Find In the Attribute Definition dialog box, set the attribute modes and enter tag information, ...
Read more >
How to pass pivot table extra attributes while creating a model?
Now I'm trying to create a category using user reference and trying to pass pivot table extra attributes value, but not getting success....
Read more >
Subclassing sympy symbols and functions to attach additional ...
Hello, I have a question about best practices in subclassing sympy. If there is a good tutorial out there, I would be grateful...
Read more >
Part 3 - How do I attach a User Defined Attributes (UDAs) to an ...
Under the UDA heading in the Properties Window, define the value for each attribute that you want to attach. If the attribute being...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found