belongsToMany incorrectly assumes idAttribute is a column name.
See original GitHub issueMy 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:
- Created 9 years ago
- Comments:12 (8 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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