Question

My models.json:

My models.json

"masterTrip": {
    "options": {
        "relations": {
            "trips": {
                "type": "hasMany",
                "model": "trip",
                "foreignKey": "masterTripId"
            }
        }
    }
},
"trip": {
    "options": {
        "relations": {
            "masterTrips": {
                "type": "belongsTo",
                "model": "masterTrip",
                "foreignKey": "masterTripId"
            }
        }
    }
},

But I do not get the relation between the trip and mastertrip. can anybody explain?

Was it helpful?

Solution

There are a couple things that might be the problem. Here is what comes to mind:

  • You should only need the foreign key on the model that belongsTo the other one. (In this case, that would be trip.)
  • Have you actually created trips underneath masterTrip? (Either in the code itself or via the API?) I know this sounds silly, but the context wasn't clear enough for me to tell if you had created the sample data or not.
  • It sounds like you might actually be getting data when you do a GET on /masterTrip/1/trips Is that right? If so , then that sounds like the correct behavior.

I'm still relatively new to LoopBack myself, but I'm not sure that filter[include]=belongsToRelationName is the correct way to get the data you want. Technically, you are just looking for the associated array of hasMany data, right? In this case, trips that belongTo masterTrip. The RESTful way to get that would be masterTrip/{id}/trips

Hope one of those helps.

OTHER TIPS

Your "belongs to" relation name is not singular. It should be singular.

When you making " belongs to " relation name is singular and for hasMany your relation name in plural. Please see the official documentation for more details -

See this working example below -

{
  "name": "Booking",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": true,
  "properties": {
    "myuserId": {
      "type": "number"
    },
    "orgin": {
      "type": "string"
    },
    "orgin_lat": {
      "type": "string"
    },
    "orgin_lon": {
      "type": "string"
    },
    "destination": {
      "type": "string"
    },
    "dest_lat": {
      "type": "string"
    },
    "dest_lon": {
      "type": "string"
    },
    "parcel_type": {
      "type": "string"
    },
    "volume": {
      "type": "string"
    },
    "weight": {
      "type": "string"
    },
    "price": {
      "type": "string"
    },
    "receiver_phn": {
      "type": "string"
    },
    "payment_mode": {
      "type": "string"
    },
    "booking_status": {
      "type": "string"
    },
    "lang": {
      "type": "string"
    },
    "booking_no": {
      "type": "string"
    },
    "cancel_reason": {
      "type": "string"
    },
     "cancel_by": {
      "type": "string"
    },
    "booking_date": {
      "type": "string"
    },
    "plan_later": {
      "type": "string"
    },
    "plan_date": {
      "type": "string"
    },
    "created": {
      "type": "string"
    },
    "modified": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
      "biddings": {
      "type": "hasMany",
      "model": "Bidding",
      "foreignKey": "bookingId"
    }
  },
  "acls": [],
  "methods": {}
}

{
  "name": "Bidding",
  "base": "PersistedModel",
  "strict": false,
  "idInjection": true,
  "options": {
    "validateUpsert": true
  },
  "properties": {
    "myuserId": {
      "type": "number"
    },
    "bookingId": {
      "type": "number"
    },
    "booking_no": {
      "type": "string"
    },
    "price": {
      "type": "string"
    },
    "message": {
      "type": "string"
    },
    "bid_date": {
      "type": "string"
    },
    "bid_time": {
      "type": "string"
    },
    "bid_status": {
      "type": "string"
    },
    "lang": {
      "type": "string"
    },
     "rate_driver": {
      "type": "number"
    },
    "created": {
      "type": "string"
    },
    "modified": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {
    "booking": {
      "type": "belongsTo",
      "model": "Booking",
      "foreignKey": "bookingId"
    },
    "myuser": {
      "type": "belongsTo",
      "model": "Myuser",
      "foreignKey": "myuserId"
    }
 
  },
  "acls": [],
  "methods": {}
}

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top