You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
HasMany Relationship Storing same id for foreign key and primary key #151
When you have a hasMany relationship, say 1 User can have many Orders, and you use the function for adding a new order for this user. I am getting a weird occurrence where the FK(userId) for the user in the Orders collection is the exact same as the PK in the Orders table.
For example, when I use this method:
User.order.create({id: User.getCurrentId(),order: order}).$promise.then(function(order){$log.debug('updated order on backend to: ',order);},function(reason){console.log('cant update order because: ',reason);});
which is created with angular SDK. The result in the database is
As one can see both the Foreign Key(userId) and the Primary Key(_id) are exactly the same. The problem here is that, when I want to create another Order for the same customer I get a "duplicate key" error because the PK is the same again.
Whats even more interesting when I use the swagger interface and generate the REST call manually using:
POST /users/{id}/orders
I get different PKs and FKs. Dont know whats going on here, but I dont think its the model declaration in my json files on node because swagger is doing this correctly.
I am afraid you are using the API incorrectly. Here is the correct way how to create a related instance:
User.order.create({id: User.getCurrentId()},order).$promise.then(function(order){$log.debug('updated order on backend to: ',order);},function(reason){console.log('cant update order because: ',reason);});
See this unit-test and the API docs generated in your services files for more information. Alternatively, take a look at User.accessTokens.create docs, it is using the same HasMany relationship.
Activity
bajtos commentedon Jun 1, 2015
I am afraid you are using the API incorrectly. Here is the correct way how to create a related instance:
See this unit-test and the API docs generated in your services files for more information. Alternatively, take a look at User.accessTokens.create docs, it is using the same HasMany relationship.
britztopher commentedon Jun 1, 2015
Thanks @bajtos, sorry for the misread in docs, as I thought the params and the payload had to be within the same object.
bajtos commentedon Jun 1, 2015
No worries, I was glad to help :)