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.

belongsToMany incorrectly assumes idAttribute is a column name.

See original GitHub issue

My column names are snake_case, but my attribute names are camelCase. My database’s primary key is entity_id, for instance, the accounts table has a primary key of account_id.

The belongsToMany association is incorrectly using the camelized idAttribute to join the tables.


// the code below will run this query. 
// Notice the incorrect column name in the ON clause:
// 'select "foos".*, "bars_foos"."bar_id" as "_pivot_bar_id", "bars_foos"."foo_id" as "_pivot_foo_id" 
// from "foos" inner join "bars_foos" on "bars_foos"."foo_id" = "foos"."fooId" 
// where "bars_foos"."bar_id" = $1'

var Foo = bookshelf.Model.extend({
    tableName: 'foos',
    idAttribute: 'fooId', // column name is actually foo_id.

    format: function(attrs) {
        return {
            'foo_id': attrs.fooId
        };
    },

    parse: function(record) {
        return {
            fooId: record['foo_id']
        };
    }
});

var Bar = bookshelf.Model.extend({
    tableName: 'bars',
    foos: function() {
        return this.belongsToMany(Foo, 'bars_foos', 'bar_id', 'foo_id');
    }
});

var foos = Bar.forge({id: 1}).related('foos').fetch();


Issue Analytics

  • State:closed
  • Created 9 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

1reaction
tgriessercommented, Jun 21, 2014

Yeah, this is incorrect. I’m going to actually provide support for camel casing the keys directly in the library in the upcoming minor version, but in the meantime I’ll see if I can fix this one. Thanks for the test case.

0reactions
crunchtime-alicommented, Jul 19, 2017

The project leadership of Bookshelf recently changed. In an effort to advance the project we close all issues older than one year.

If you think this issue needs to be re-evaluated please post a comment on why this is still important and we will re-open it.

We also started an open discussion about the future of Bookshelf.js here https://github.com/bookshelf/bookshelf/issues/1600. Feel free to drop by and give us your opinion. Let’s make Bookshelf great again

Read more comments on GitHub >

github_iconTop Results From Across the Web

using name column in belongsToMany relation instead of id
Basically App\Role is the relation model. team_users is the pivot table name. user_id is the foreign pivot key (So in team_users there will...
Read more >
through() does not work as expected · Issue #1025 · bookshelf ...
The admins table referenced in the models/Admin.js model has a user_id column and an org_id column. When I invoke Org.forge({ id: 1 }) ......
Read more >
Eloquent ORM - Laravel - The PHP Framework For Web Artisans
So, in this case, Eloquent will assume the User model stores records in the users ... will also assume that each table has...
Read more >
lib/relation.js - Bookshelf.js
parentResponse; // The `belongsToMany` and `through` relations have joins & pivot columns. if (this.isJoined()) this.joinClauses(knex); // Call the function ...
Read more >
Eloquent ORM (Laravel 4.0)
Note: Eloquent will also assume that each table has a primary key column named id . You may define a primaryKey property to...
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