loopback-component-jsonapi icon indicating copy to clipboard operation
loopback-component-jsonapi copied to clipboard

handle hasAndBelongsToMany relations on POST

Open behnoodk opened this issue 9 years ago • 4 comments

When adding a record through POST which has a hasAndBelongsToMany with another model, the component is acting strange. I'm thinking even if this is not supported yet, it should not behave this way:

//loopback models/article.json
{
  "name": "article",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    //....
  },
  "validations": [],
  "relations": {
    "category": {
      "type": "belongsTo",
      "model": "category",
      "foreignKey": ""
    },
    "tags": {
      "type": "hasAndBelongsToMany",
      "model": "tag"
    }
  },
  "acls": [],
  "methods": {}
}

//Request sent by Ember JSONAdapter
POST /api/articles
{
  "data": {
    "attributes": {
      "title": "test",
      "slug": "test",
      "date": "Thu Feb 18 2016 17:03:20 GMT+0000",
      "text": "<p>test</p>",
      "posterimageurl": "test"
    },
    "relationships": {
      "tags": {
        "data": [{
          "type": "tags",
          "id": "4kDzGcCcl"
        }, {
          "type": "tags",
          "id": "56c5dbd15b282680a6d89ab0"
        }]
      },
      "category": {
        "data": {
          "type": "categories",
          "id": "41Mi5B35x"
        }
      }
    },
    "type": "articles"
  }
}
//response given by loopback
{
  "data": {
    "type": "articles",
    "relationships": {
      "category": {
        "links": {
          "related": "http://dev:3000/api/articles/NJDrKxyox/category"
        },
        "data": {
          "type": "categories",
          "id": "41Mi5B35x"
        }
      },
      "tags": {
        "links": {
          "related": "http://dev:3000/api/articles/NJDrKxyox/tags"
        }
      }
    },
    "id": "NJDrKxyox",
    "attributes": {
      "title": "test",
      "slug": "test",
      "date": "2016-02-18T17:03:20.000Z",
      "text": "<p>test</p>",
      "posterimageurl": "/api/containers/common/download/179.41eLgvacx.jpg"
    },
    "links": {
      "self": "http://dev:3000/api/articles/NJDrKxyox"
    }
  }
}

So loopback handles the belongsTo relationship correctly. but the hasAndBelongsToMany is behaving strange. first there is nothing in the response and second when I check the db, it has updated the field articleId of all the included tags to the id of created article. and when a new article is created, this articleId gets overwritten for all the tags provided in the request.

behnoodk avatar Feb 18 '16 18:02 behnoodk

Yea there are definitely some issues with hasandbelongstomany. Also any use of "through"

We ideally need to start with some failing tests and take it from there.

Do you have any time to do this?

digitalsadhu avatar Feb 21 '16 07:02 digitalsadhu

Ps thanks for reporting! (and sorry for the slow reply)

digitalsadhu avatar Feb 21 '16 07:02 digitalsadhu

I had some other issues with loopback and decided to write my API from scratch with express since it's a small API. Unfortunately I do not have time to do this. I think at the very least it would be good to mention this in the README. this component does a great job for all belongsTo relationships, but acts unexpectedly for other types of relationships.

behnoodk avatar Feb 22 '16 13:02 behnoodk

@behnoodk how do you send this kind of request? I can only send with the attributes content as body.

thedanheller avatar Mar 01 '18 21:03 thedanheller