Eesa
Eesa

Reputation: 2859

Loopback - EmbedsOne relation producing error

I have following two models

Nurse

Room

A room has belongsTo relation with the nurse model as defined below:

"relations": {
        "nurse": {
            "model": "Nurse",
            "type": "belongsTo",
            "foreignKey": "nid"
        }
    }

which works fine and produces the data on following urls

http://localhost:3000/api/Rooms/1

http://localhost:3000/api/Rooms/1/nurse

but when I try the embedsOne relation as defined below:

"relations": {
        "nurse": {
            "model": "Nurse",
            "type": "embedsOne",
            "foreignKey": "nid"
        }
    }

and try accessing url at

http://localhost:3000/api/Rooms/1

I get the following error:

message: "ER_BAD_FIELD_ERROR: Unknown column '_nurse' in 'field list'",

thoughts?

Upvotes: 3

Views: 477

Answers (1)

Piotr Labunski
Piotr Labunski

Reputation: 1648

embedsOne relation does not have foreignKey. You should use "property" as is shown in documentation: (https://docs.strongloop.com/display/public/LB/Embedded+models+and+relations#Embeddedmodelsandrelations-EmbedsOne)

"relations": {
  "address": {
    "type": "embedsOne",
    "model": "Address",
    "property": "billingAddress"
  }
}

then your object will look like

 {
  id: 1,
  name: 'John Smith',
  billingAddress: {
    street: '123 Main St',
    city: 'San Jose',
    state: 'CA',
    zipCode: '95124'
  }
}

Upvotes: 1

Related Questions