Search code examples
foreign-key-relationshiploopbackjsloopback

Parent child relationships in Loopback


I have two loopback models, say Product and Service. They are related to a parent model Asset. The Asset has an AssetType which says if the current object is a Product or a Service. How do I model the entity relationships for this case?


Solution

  • Use polymorphic relations https://loopback.io/doc/en/lb3/Polymorphic-relations.html

    "parent": {
        "type": "belongsTo",
        "polymorphic": {
            "foreignKey": "parentId",
            "discriminator": "parentType"
        }
    }