1

I don't know if I'm just blind or something but how can I do the following:

I have a User model with a hasOne relation to a UserData model. I only want one property of UserData directly in the results of User.

The relation in User looks like this:

"relations": {
  "userData": {
    "type": "hasOne",
    "model": "UserData"
  }
}

And the default scope in User:

"scope": {
  "include": "userData"
}

So the result for one User is:

[
  {
    "id": 5,
    "email": "example@example.com",
    "name": "Example",
    "userData": {
      "id": 5,
      "birthdate": "1971-09-06T00:00:00.000Z"
    }
  }
]

But what I want is this:

[
  {
    "id": 5,
    "email": "example@example.com",
    "name": "Example",
    "birthdate": "1971-09-06T00:00:00.000Z"
  }
]

How can I achive this?

Edit:

The two model definitions:

ChiliUser:

{
  "name": "ChiliUser",
  "base": "ChiliUserData",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "mysql": {
      "table": "person"
    }
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "email": {
      "type": "string"
    },
    "password": {
      "type": "string"
    },
    "vorname": {
      "type": "string"
    },
    "name": {
      "type": "string"
    },
    "spitzname": {
      "type": "string"
    },
    "strasse": {
      "type": "string"
    },
    "plz": {
      "type": "number"
    },
    "ort": {
      "type": "string"
    },
    "geolat": {
      "type": "string"
    },
    "geolng": {
      "type": "string"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}

ChiliUserData:

{
  "name": "ChiliUserData",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "mysql": {
      "table": "person_data"
    }
  },
  "properties": {
    "id": {
      "type": "number"
    },
    "person_id": {
      "type": "number"
    },
    "birthdate": {
      "type": "date"
    }
  },
  "validations": [],
  "relations": {},
  "acls": [],
  "methods": []
}
toXel
  • 165
  • 3
  • 9

1 Answers1

1

Are you using the built-in User model? If you are, you can simply extend it with your UserData model using base: "User", and the underlying schema will be as you desire:

{
  "name": "UserData",
  "base": "User",
  "properties": {}
  ...
}

But this setup would mean you'd use UserData over User in your code. Since the built-in User model gets in the way, I'd recommend a different word like Person or Customer, in which case it would look like:

{
  "name": "Person",
  "base": "User",
  "properties": {
    ...extra user data properties only...
  }
  ...
}

This way, only Person will have all the extra "user data" fields on it, but will also include all the fields/methods defined for User inside node_modules/loopback/common/models/User.json. If you're using MySQL, the Person table will be created with the combination of fields from both User and Person.

notbrain
  • 3,366
  • 2
  • 32
  • 42
  • Thanks for the answer! No I don't use the built-in User model. I just renamed it here in the question for better readability. The models are `ChiliUser` and `ChiliUserData` in my app. Is there an alternative way for doing this? Because the `ChiliUser` model is the main model in this case and I need it that way. – toXel Aug 17 '15 at 21:41
  • I think you can get your desired effect by defining the `ChiliUser` model with `"base": "ChiliUserData"`. – notbrain Aug 17 '15 at 22:44
  • I've added the model definitions in my question above. When I execute the default `GET` method now for `ChiliUser` then StrongLoop gives me this error: `ER_BAD_FIELD_ERROR: Unknown column 'person_id' in 'field list'`. Do I have to do something additional to just adding `"base": "ChiliUserData"`? – toXel Aug 19 '15 at 16:41
  • Did you create a Person model at one point? That error usually means loopback is expecting to see a person_id column from your database, but it's not been created. It might be an artifact that you can remove because it doesn't make sense unless you explicitly created a relation to a Person model. – notbrain Aug 19 '15 at 18:40
  • Both models are connected to a MySQL database. The table behind `ChiliUserData` has three columns: id, person_id and birthdate. It should be alright as far as I understand it. – toXel Aug 19 '15 at 19:32
  • One way to find out is to drop your DB and run a migration to recreate the entire database based on the model.json info. `dataSource.automigrate(['ChiliUserData', 'ChiliUser'], function(err){...})`. Then the DB will reflect the state the API expects. It doesn't make sense to have `person_id` there if you do not have either a `Person` model or an explicit relation. – notbrain Aug 24 '15 at 23:41
  • One more thing: using `'base': 'ChiliUserData'` for ChiliUser, the API expects only 1 table to be created: `ChiliUser`, which will have columns from both models. – notbrain Aug 24 '15 at 23:47