Specify primary key for relations

601 views
Skip to first unread message

Charles Naccio

unread,
Oct 8, 2014, 2:43:58 PM10/8/14
to loopb...@googlegroups.com
I was trying to use relations for this use case, but I'm starting to think it may not be possible.

Here's what I'm trying to do.


Tables

  • sentEmails
    • id
    • emailId

  • sentEmailViews
    • id
    • emailId


sentEmails hasMany sentEmailViews, and sentEmailViews belongsTo sentEmails but the relation uses the emailId to establish the relationship. Is this possible in LoopBack? Any help is appreciated!


I'm trying to query sentEmails including sentEmailViews for each sentEmails that's found.


Raymond Feng

unread,
Oct 8, 2014, 4:13:38 PM10/8/14
to Charles Naccio, loopb...@googlegroups.com
Did you check out http://docs.strongloop.com/display/LB/Creating+model+relations? There are examples.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

--
You received this message because you are subscribed to the Google Groups "LoopbackJS" group.
To unsubscribe from this group and stop receiving emails from it, send an email to loopbackjs+...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Charles Naccio

unread,
Oct 9, 2014, 12:27:22 PM10/9/14
to loopb...@googlegroups.com, charles.trinit...@gmail.com
I sure did, and there is no example that covers my use case. What I want to be able to do is specify the primary key as well as the foreign key. Any help is appreciated; I'd really like to get this working.

On Wednesday, October 8, 2014 3:13:38 PM UTC-5, Raymond Feng wrote:
Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

Raymond Feng

unread,
Oct 9, 2014, 1:06:34 PM10/9/14
to Charles Naccio, loopb...@googlegroups.com
Can I assume sentEmailViews.emailId is the FK and  sentEmails.emailId is the PK?

Sent from my iPhone 6 Plus

Charles Naccio

unread,
Oct 9, 2014, 3:44:06 PM10/9/14
to loopb...@googlegroups.com, charles.trinit...@gmail.com
Yes that is my case...

On Thursday, October 9, 2014 12:06:34 PM UTC-5, Raymond Feng wrote:
Can I assume sentEmailViews.emailId is the FK and  sentEmails.emailId is the PK?

Sent from my iPhone 6 Plus

Felipe Figueroa

unread,
Oct 15, 2014, 6:40:50 AM10/15/14
to loopb...@googlegroups.com, charles.trinit...@gmail.com
But if that was the case... then why do you have an id field in sentEmail?

Why don't just declare the id property and map it to the emailId field of your table?

Charles Naccio

unread,
Dec 2, 2014, 11:57:13 AM12/2/14
to loopb...@googlegroups.com, charles.trinit...@gmail.com
Unfortunately I don't have control over the database. Is there no way to specify which field to user for the primary key instead of the default modelNameId?

Sanjay Pandey

unread,
Feb 18, 2015, 7:18:31 AM2/18/15
to loopb...@googlegroups.com, charles.trinit...@gmail.com
hey Charles,

Did you get any way to reference two models through a property other than primary key? I am also looking for solution to this problem.

Raymond Feng

unread,
Feb 18, 2015, 12:02:40 PM2/18/15
to Sanjay Pandey, loopb...@googlegroups.com, charles.trinit...@gmail.com
Referencing other properties as PK is not supported at the moment. But it should be fairly simple to implement the feature.

1. Extend the relation definition to allow ‘primaryKey’, such as {“primaryKey”: “myPK”}.
2. Honor the primaryKey customization and default it to the id properties of the target model.

Contributions are welcome!

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc.

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.

On Feb 18, 2015, at 4:18 AM, Sanjay Pandey <vigya...@gmail.com> wrote:

hey Charles,

Did you get any way to reference two models through a property other than primary key? I am also looking for solution to this problem.

Van Carney

unread,
Oct 4, 2015, 2:57:28 PM10/4/15
to LoopbackJS, vigya...@gmail.com, charles.trinit...@gmail.com
Wow -- after days of trying to make this simple use case that would be a snap in any other ORM framework, here I finally find Ray's little post on google groups.
You know, Loopback has a very major failing -- LACK OF DOCUMENTATION. Things like this ARE NOT DOCUMENTED. And what Documentation there is, is half-assed, poorly written and often times confusing.

Now that Strongloop has been acquired by IBM, maybe they can bring in a Techincal Writer and put some time and money into making a usable set of docs for their framework. Because believe it or not APIs and Frameworks are only as good as their documentation -- if people can't figure out how to use it, THEY WONT.

Loopback has lots of potential -- but that potential is harmed by the lack of Docs.

Sorry if I'm a bit rattled here -- but thanks to your lack of docs, I've spent many hours trying to make your framework do simple things such as this and now am past due and working at a loss thanks to this -- my fault for using Loopback a framework not ready for production use. I'd have had a much better time writing an API from scratch or just using Rails instead.

eric.b...@carbip.com

unread,
Oct 14, 2015, 11:46:33 AM10/14/15
to loopb...@googlegroups.com, vigya...@gmail.com, charles.trinit...@gmail.com
Hi, any news on this loopback team? the post trail is old now and I guess there were some additions on this as there are a bunch of id parameters to play with such as 'generated'.

i'm using mongo with this model configuration:

{
  "name": "myModel",
  "plural": "myModels",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "other_id": {
        "type": "string",
        "generated":false,
        "id":true,
        "index": {"unique": true},
        "required": true
    },
...
}

and i can't reference instances of this model parent using this other_id of mine as the primary key for related model foreign key.

furthermore, if i query myModel, other_id is populated with the stringified representation of mongo _id : "other_id ": "561254afcd2a80b21e432064",  :-||... loopback is definitely overriding my homemade id here !

should i try something else or is the feature still not available ?

Eric

Raymond Feng

unread,
Oct 14, 2015, 12:14:16 PM10/14/15
to loopb...@googlegroups.com, vigya...@gmail.com, charles.trinit...@gmail.com
We do allow the ‘primaryKey’ customization for relations now so that your relation can use a different PK other than model’s PK.

Thanks,

---
Raymond Feng
Co-Founder and Architect @ StrongLoop, Inc. (An IBM Company)

StrongLoop makes it easy to develop APIs in Node, plus get DevOps capabilities like monitoring, debugging and clustering.
On Oct 14, 2015, at 8:46 AM, eric.b...@carbip.com wrote:

Hi, any news on this loopback team? the post trail is old now and I guess there were some additions on this as there are a bunch of id parameters to play with such as 'generated'.

i'm using mongo with this model configuration:

{
  "name": "myModel",
  "plural": "myModels",
  "base": "PersistedModel",
  "idInjection": false,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "other_id": {
        "type": "string",
        "generated":false,
        "id":true,
        "index": {"unique": true},
        "required": true
    },
...
}

and i can't reference instances of this model parent using this other_id of mine as the primary key for related model foreign key.

Eric Barault

unread,
Oct 14, 2015, 1:10:10 PM10/14/15
to loopb...@googlegroups.com, vigya...@gmail.com, charles.trinit...@gmail.com
Hi Raymond,
thanks for feedback.

then am i missing something in my model config ?

loopback is definitely rewriting my custom key to the mongo _id each time i query on it, and i suspect this is effect of something underlying that prevents me from getting the relation working with something else than the parent's PK as the children's FK

Eric
You received this message because you are subscribed to a topic in the Google Groups "LoopbackJS" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/loopbackjs/F1JcB4bTtxM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to loopbackjs+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/loopbackjs/A72E23BA-8787-46EA-B7B0-A525D7487C0B%40strongloop.com.

For more options, visit https://groups.google.com/d/optout.

--
Reply all
Reply to author
Forward
0 new messages