2

I have a feathers api set up using feathers-sequelize to persist to a MySQL database.

I've got the datamodel set up and can see the relevant tables are created.

const Sequelize = require('sequelize');

module.exports = function (app) {
  const sequelizeClient = app.get('sequelizeClient');

  const orders = sequelizeClient.define('orders', {
    id: {
      type: Sequelize.UUID,
      primaryKey: true,
      defaultValue: Sequelize.UUIDV4

  }, {
    classMethods: {
      associate (models) {
          this.belongsToMany(models.users, {through: "offers", foreignKey: "orderId", otherKey: "userId"});
      }
    }
  });

  return orders;
};

How do I actually create an association of offers? I've tried something like this:

const orders = hook.app.service("orders");

order.offers.push(user.id);
orders.patch(order.id, order);

but it doesn't seem to have any effect

alt
  • 2,356
  • 2
  • 21
  • 28

1 Answers1

0

You shouldn't need to patch with a many-to-many. Simply use the Sequelize association method:

order.addUser(user.id)

To return the association you might need to do an order.reaload() in your after hook.

YiSh
  • 347
  • 1
  • 4
  • 12