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

Failed withRelated three tables #82

Closed
samsaga2 opened this issue Oct 9, 2013 · 4 comments
Closed

Failed withRelated three tables #82

samsaga2 opened this issue Oct 9, 2013 · 4 comments

Comments

@samsaga2
Copy link

samsaga2 commented Oct 9, 2013

I don't know why Bookshelf is fetching only the first relation.

"use strict";

var Bookshelf = require('bookshelf');

var db = Bookshelf.initialize({
  debug: true,
  client: 'postgres',
  connection: {
    host: '169.254.1.4',
    port: 5432,
    user: 'postgres',
    password: '',
    database: 'canela'
  }
});

var Compta = db.Model.extend({
  tableName: 'comptes'
});

var Client = db.Model.extend({
  tableName: 'fitxes_clients',
  idAttribute: 'id_fitxa',
  compta: function() {
    return this.belongsTo(Compta, 'id_compta');
  }
});

var Fitxa = db.Model.extend({
  tableName: 'fitxes',
  client: function() {
    return this.belongsTo(Client, 'id_fitxa');
  }
});

new Fitxa().fetch({
  withRelated: ['client','client.compta']
})
.then(function(data) {
  console.log(data);
});

But Bookshelf only makes two queries. The client.compta it's never fetched.

{ sql: 'select "fitxes".* from "fitxes" limit 1', bindings: [], __cid: '__cid2' } { sql: 'select "fitxes_clients".* from "fitxes_clients" where "id_fitxa" in (?)', bindings: [ undefined ], __cid: '__cid3' }

And yes, idAttribute on table client really sucks.

@tgriesser
Copy link
Member

It looks like it's not retrieving anything from the "client" fetch - bindings: [ undefined ] therefore it wouldn't get to the compta portion of the query... does there need to be an idAttribute set for the Fixta table?

Could you give me an shortened example schema (table keys / values) in the db so I can take a look at reproducing?

@samsaga2
Copy link
Author

I was writing a better example and you're right. The bindings: [ undefined ] means my keys are wrong.

But my real problem it's that Bookshelf doesn't support composited keys. Something like:

var Table1 = db.Model.extend({
  tableName: 'table1',
  idAttribute: ['key1', 'key2']
});

var Table2 = db.Model.extend({
  tableName: 'table2',
  test: function() {
    return this.belongsTo(Table1, ['key1_id', 'key2_id']);
  }
});

Thanks for your help.

@tgriesser
Copy link
Member

Yeah, I'd like to be able to support composite keys at some point, let me know if you have any ideas on how these could be easily added.

@tgriesser
Copy link
Member

Moving to #86.

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

No branches or pull requests

2 participants