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

Creating resource relationship linkages during a resource create

Open devotox opened this issue 8 years ago • 6 comments

Creating a resource relationship questions

  • How should the data look like for the relationship. Just a regular POJO or a full JSON API spec set i.e. with { id: 123, attributes: { name: ... } }

  • The reason why the relationship is null is because Ember sends any relationship with no data as null and expects the backend to handle it

  • It seems the problem is that it is trying to set nothing where userid=$1

Versions

"loopback": "2.38.1",
"loopback-component-jsonapi": "2.2.1",
"loopback-connector-postgresql": "2.7.0",

Error

database_1         | STATEMENT:  UPDATE "public"."profile" SET  WHERE "userid"=$1
api_1              | 22:06:55 0|api      | { error: syntax error at or near "WHERE"
api_1              | 22:06:55 0|api      |     at Connection.parseE (/tmp/api/node_modules/pg/lib/connection.js:572:11)
api_1              | 22:06:55 0|api      |     at Connection.parseMessage (/tmp/api/node_modules/pg/lib/connection.js:396:17)

Data sent

{
    "data": {
        "attributes": {
            "email": "[email protected]",
            "realm": null,
            "status": null,
            "username": "devotox0.9883987623877892",
            "password": "dev",
            "challenges": null,
            "lastupdated": null,
            "emailverified": false,
            "created": "2017-06-08T21:52:35.513Z"
        },
        "relationships": {
            "profile": {
                "data": null
            }
        },
        "type": "users"
    }
}

devotox avatar Jun 08 '17 22:06 devotox

Interesting. Yes it certainly looks like its because its trying to set nothing in that update statement. Does this just happen for creates? I assume so. I'll put together a test case and see if I can replicate.

Thanks for reporting

digitalsadhu avatar Jun 09 '17 19:06 digitalsadhu

Wrote a quick test. No failures when using the memory connector. Probably to be expected. I'm a bit surprised I haven't run into this before. Does just simply defining a relationship to profile on user in ember mean than whenever you create a user it sets that null profile data?

digitalsadhu avatar Jun 09 '17 19:06 digitalsadhu

@digitalsadhu Yes just having a belongsTo relationship without even actually populating it will always create a null value. Also if there is profileData it does not actually send the data just the id there so it would look something like this

Data sent

{
    "data": {
        "attributes": {
            "email": "[email protected]",
            "realm": null,
            "status": null,
            "username": "devotox0.9883987623877892",
            "password": "dev",
            "challenges": null,
            "lastupdated": null,
            "emailverified": false,
            "created": "2017-06-08T21:52:35.513Z"
        },
        "relationships": {
            "profile": {
                "data": 1
            }
        },
        "type": "users"
    }
}

devotox avatar Jun 18 '17 11:06 devotox

Hmm, Something fishy going on there. Do you mean ember is sending that id?

"relationships": {
   "profile": {
     "data": 1
   }
},

If thats the case as I'm sure you know that is invalid.

It should send either:

"relationships": {
   "profile": {
     "data": null
   }
},

or

"relationships": {
   "profile": {
     "data": {
        "id": 1,
        "type": "profiles"
      }
   }
},

Or did you mean that the server is responding with that invalid data?

Are you able to paste model definitions, server response and client payloads?

digitalsadhu avatar Jun 18 '17 19:06 digitalsadhu

I will try and get you this by tomorrow or monday just saw your reply now. But even before the problem with how it is sending back the relationships when there is data we should handle what happens when there the data is null by loopback

devotox avatar Jul 08 '17 16:07 devotox

Cool thanks! I'm on holiday for the next 2 weeks. Probably wont be at my computer a whole lot so no rush.

digitalsadhu avatar Jul 08 '17 18:07 digitalsadhu