cesardeazevedo / sails-hook-sequelize-blueprints

Sails blueprints for sequelize ORM
MIT License
32 stars 19 forks source link

Belongs To Many does not work with insert #11

Closed ya-kostik closed 8 years ago

ya-kostik commented 8 years ago

Hi! I have some problem with lazy M:M inserts;

For example: I have a Product model

module.exports = {
  attributes: {
    title: {
      type: Sequelize.STRING,
      allowNull: false,
      unique: true
    }
  },
  associations: function() {
    Product.belongsToMany(Tag, {
      as: 'tags',
      to: 'products', // must be named as the alias in the related Model
      through: 'ProductTags',
      foreignKey: {
        name: 'product',
        as: 'tags'
      }
    });
  },
  options: {
    tableName: 'Product',
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
};

And I have a Tag model:

module.exports = {
  attributes: {
    title: {
      type: Sequelize.STRING,
      allowNull: false
    }
  },
  associations: function() {
    Tag.belongsToMany(Product, {
      as: 'products',
      to: 'tags', // must be named as the alias in the related Model
      through: 'ProductTags',
      foreignKey: {
        name: 'tag',
        as: 'products'
      }
    });
  },
  options: {
    tableName: 'Tag',
    classMethods: {},
    instanceMethods: {},
    hooks: {}
  }
};

When I'm try to create Product with tags array (POST):

{
  "title": "Роджер",
  "tags": [
    {
      "createdAt": "2015-11-26T10:49:04.000Z",
      "title": "Ткань",
      "updatedAt": "2015-11-26T13:35:33.000Z"
    },
    {
      "createdAt": "2015-11-26T10:49:04.000Z",
      "title": "Череп",
      "updatedAt": "2015-11-26T13:35:33.000Z"
    },
  ]
}

Product was create, but Tags did not.

If tags will have id, this did not work too.

What am I doing wrong?

jesusbv commented 8 years ago

hi @ya-kostik , atm there is no implementation for lazy inserts

the way we did it was either both of following

There should not be a problem to implement lazy insert tbh so if you really need it, get in touch.

BR

jesusbv commented 8 years ago

@ya-kostik hey, I see you closed it, havent got time to back to this actually.

Let me know if you fix it