Skip to content

Instantly share code, notes, and snippets.

@srushti
Last active August 29, 2015 14:09
Show Gist options
  • Save srushti/d170c57abb87a781e90d to your computer and use it in GitHub Desktop.
Save srushti/d170c57abb87a781e90d to your computer and use it in GitHub Desktop.
var Customer = Bookshelf.Model.extend({
    tableName : 'customers',
    accounts  : function() {
        return this.hasMany(Account, 'customerId');
    }
});

var Account = Bookshelf.Model.extend({
    tableName   : 'accounts',
    customer    : function() {
        return this.belongsTo(Customer, 'customerId');
    },
});

I'm trying to get all accounts where accounts.name = 'Smith' and customers.pro = true.

knex solution:

    knex('accounts')
      .join('customers', 'customers.id', 'accounts.customerId')
      .where('customers.pro', '=', true)
      .where('accounts.name', '=', 'Smith')
      .select()
      .then (accounts) ->
        // success code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment