Join UpdatePivot for Not Notull column

Using bookshelfjs, I would like to add an entry to the connection table containing an extra column with a null value.

For example, the user table and account table will have an account connection table.

var Account = Bookshelf.Model.extend({
  tableName: 'accounts'
});

var User = Bookshelf.Model.extend({

  tableName: 'users',

  accounts: function () {
    return this.belongsToMany(Account);
  }
});

The join table also has an NOT NULL order column.

CREATE TABLE accounts_users
(
  account_id integer NOT NULL,
  user_id integer NOT NULL,
  "order" integer NOT NULL,
  CONSTRAINT accounts_users_account_id_foreign FOREIGN KEY (account_id)
      REFERENCES accounts (id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE,
  CONSTRAINT accounts_users_user_id_foreign FOREIGN KEY (user_id)
      REFERENCES users (id) MATCH SIMPLE
      ON UPDATE CASCADE ON DELETE CASCADE
)

Is it possible to attach an entry in Users to an entry in accounts and at the same time set the order value?

Each of them can be done separately using:

user.related("accounts").attach(<USER_ID>);

and

user.related("accounts").updatePivot({order: 1}); // with WHERE clause.

, NOT NULL. ? Knex.Raw, , , .

+4
1

/ .attach, . . GitHub:

https://github.com/tgriesser/bookshelf/issues/134

user.related('accounts').attach({ account_id: 42, user_id: 84, order: 1})
+7

Source: https://habr.com/ru/post/1529740/


All Articles