Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

handle hasAndBelongsToMany relations on POST #114

Open
behnoodk opened this issue Feb 18, 2016 · 4 comments
Open

handle hasAndBelongsToMany relations on POST #114

behnoodk opened this issue Feb 18, 2016 · 4 comments

Comments

@behnoodk
Copy link

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.

@digitalsadhu
Copy link
Owner

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
Copy link
Owner

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

@behnoodk
Copy link
Author

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.

@thedanheller
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants