Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

belongsToMany incorrectly assumes idAttribute is a column name. #397

Closed
foster opened this issue Jun 20, 2014 · 12 comments
Closed

belongsToMany incorrectly assumes idAttribute is a column name. #397

foster opened this issue Jun 20, 2014 · 12 comments

Comments

@foster
Copy link

foster commented Jun 20, 2014

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();
@tgriesser tgriesser added the bug label Jun 21, 2014
@tgriesser
Copy link
Member

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.

@foster
Copy link
Author

foster commented Jun 22, 2014

Nice, thanks.

It seems that my problem is really specific to id, since other fields are .formatted before insert or update. I encountered a similar problem that .save() relies on idAttribute as a column name. Would it break abstraction too much for models to have an idColumn property?

@coolaj86
Copy link
Contributor

coolaj86 commented Aug 8, 2014

has this been fixed? I think it just bit me too.

is there a workaround?

@peteut
Copy link

peteut commented Jan 18, 2015

I had a related issue with collection#create (.related('related').create({ })), saveConstraints returns model.set(data) but should return model.set(model.parse(data)).

@rhys-vdw
Copy link
Contributor

@peteut This is still broken in the current version.

@peteut
Copy link

peteut commented Apr 24, 2015

master...peteut:4b15b012707edb8c0c9250380a8ec73f3cef6c91 worked for me. Maybe this is not related?

@rhys-vdw
Copy link
Contributor

rhys-vdw commented May 4, 2015

It may do, I can check if you like. Is there a reason why it hasn't been merged yet?

@peteut
Copy link

peteut commented May 4, 2015

I was too lazy and didn't create a test case and pull request. Sorry for that.
Could you please give it a try? Thanks a lot!

On Monday, May 4, 2015 2:28 PM, Rhys van der Waerden notifications@github.com wrote:

It may do, I can check if you like. Is there a reason why it hasn't been merged yet?

Reply to this email directly or view it on GitHub.

@rhys-vdw
Copy link
Contributor

rhys-vdw commented Jun 7, 2015

@peteut Finally got the test environment set up. PR: #771.

@LordSputnik
Copy link
Contributor

Just wondering what the status of this is? #771 doesn't seem to have fixed it - not sure if it was meant to. Thanks!

@rhys-vdw
Copy link
Contributor

rhys-vdw commented Sep 2, 2015

I had a look at it last night. It's fixable but will require a bit of a
change. #771 fixed a different problem that was discussed in this issue.

On Thu, 3 Sep 2015 8:14 am Ben Ockmore notifications@github.com wrote:

Just wondering what the status of this is? #771
#771 doesn't seem to have
fixed it - not sure if it was meant to. Thanks!


Reply to this email directly or view it on GitHub
#397 (comment)
.

vellotis added a commit to vellotis/bookshelf that referenced this issue Jun 14, 2016
Current specification of belongsToMany has a bug. Its `otherKey` and `foreignKey` parameter description are mistakenly partially swapped.

Many issues have come up because of this problem. eg. bookshelf#397, bookshelf#1031, bookshelf#1276
@crunchtime-ali
Copy link
Member

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 #1600. Feel free to drop by and give us your opinion.
Let's make Bookshelf great again

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants