loopback icon indicating copy to clipboard operation
loopback copied to clipboard

Multiple foreignkeys possibility in Model Relation?

Open chojowliu opened this issue 10 years ago • 30 comments

In the loopback model relations section, you can specificity a foreignKey to be referenced in the other model. As far as I know, It will only take one field, but what about I have a composite primary key and want to be referenced in the other model?

Can I do something like?

"foreignKey":["id1","id2"]

chojowliu avatar Feb 17 '16 21:02 chojowliu

@chojowliu I think we support composite id, see docs here: https://docs.strongloop.com/display/public/LB/Model+definition+JSON+file#ModeldefinitionJSONfile-CompositeIDs But "the composite id is not fully supported for the *byId methods, such as findById. " see details

I am not sure is composite foreign key supported ATM, @raymondfeng could you confirm it? Thanks a lot.

jannyHou avatar Feb 17 '16 22:02 jannyHou

@jannyHou I do know loopback support composite id, but not in the relation section

"relations": { "supplier_acct": { "type": "hasOne", "model": "account", "foreignKey": "supplierId" // you can't have "foreignKey":["id1","id2"], as far as I know we can only use one single column. } }

chojowliu avatar Feb 18 '16 17:02 chojowliu

anyone?

chojowliu avatar Feb 22 '16 02:02 chojowliu

@jannyHou any update on this? I search the web, it seems like people are asking for the same question I have, but none of them been answered.

chojowliu avatar Feb 22 '16 22:02 chojowliu

@raymondfeng could you confirm whether we support composite foreignKey? Thanks a lot!

jannyHou avatar Feb 23 '16 14:02 jannyHou

I would like to know also

jmls avatar Mar 23 '16 13:03 jmls

+1

jonathanwoahn avatar Apr 14 '16 11:04 jonathanwoahn

For anyone interested in multiple foreignKeys, we don't support it as first class now, I label it as a feature, hope we can fully support it in the future.

jannyHou avatar Apr 22 '16 01:04 jannyHou

+1

peterliapin avatar Jul 11 '16 11:07 peterliapin

+1

I welcome even suggestion where to look into the code of loopback and how to try to implement it on my own.

archenroot avatar Oct 14 '16 10:10 archenroot

I would mark this rather than feature as major issue, as this is preventing usage of loopback in certain scenarios.

I of course understand there is workaround by going with custom method with custom SQL queries where I can write the joins using multiple keys by hand. But it is breaking the beauty of model driven design of loopback and in general I think direct SQL queries method should be used only in edge scenarios as temporary workaround.

archenroot avatar Oct 26 '16 13:10 archenroot

@archenroot make sense. Labelled it as major

jannyHou avatar Oct 26 '16 17:10 jannyHou

Copied from https://github.com/strongloop/loopback/issues/2955#issue-190276274 @elfahxh let's keep discussion here. Support composite keys in embeds Relation:

  • Scenario:

I created two Models, Model-B is an embedded model in Mode-A, Model-A must have sharding key which have two fields. When I call such related method, Model-A.Model-B.create(), will failed. The root-cause is that, underline, it will call updateAttributes() of Model-A, while the search filter only have "_id" field, cannot define a combined multiple keys to find Model-A from database. hence, will encounter a failure.

jannyHou avatar Nov 18 '16 22:11 jannyHou

+1

skyserpent avatar Dec 12 '16 18:12 skyserpent

+1

darknos avatar Dec 15 '16 07:12 darknos

Anyone also interested in this feature please do 👍 on the first comment :) instead of adding new comments. Thanks!

jannyHou avatar Dec 15 '16 15:12 jannyHou

Any update on this?

archenroot avatar Jan 30 '17 02:01 archenroot

This story is already in backlog, we will make an investigation in the future.

jannyHou avatar Jan 31 '17 16:01 jannyHou

Possibly related: https://github.com/strongloop/loopback/issues/2046

bajtos avatar Apr 07 '17 15:04 bajtos

and also https://github.com/strongloop/loopback/issues/126

bajtos avatar Apr 07 '17 15:04 bajtos