# Models enrichment/relationship doesn't seem to work as expected

**URL:** <https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [June 19, 2020, 10:17am UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490 "2020-06-19T10:17:37Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.forestadmin.com/u/Thomas)\
**Post date:** [June 19, 2020, 10:17am UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/1 "2020-06-19T10:17:37Z")

</div>

I am currently trying to display the documents of a MongoDB database with their relations. As of now, I have the following:

User(s) → [Has outfit(s)] → Outfit(s) → [List of item(s)/product(s)] → Product(s)

We will, for the sake of this example, take only the Outfit model and the Product model. Here you can find two documents as a possible example (JSON Format):

```javascript
// Outfit document
{
    "_id" : ObjectId("5eeb712dfa3a7b9028787903"),
    "picture_url" : "https://i.skyrock.net/3911/84063911/pics/3226492571_1_2_q8PvGEGD.jpg",
    "products_ids" : [ 
        ObjectId("5eeb712dfa3a7b9028787904"), 
        ObjectId("5eeb712dfa3a7b9028787905"), 
        ObjectId("5eeb712dfa3a7b9028787906"), 
        ObjectId("5eeb712dfa3a7b9028787907")
    ]
}

// Outfit Product Document
{
    "_id" : ObjectId("5eeb712dfa3a7b9028787905"),
    "outfit_id" : ObjectId("5eeb712dfa3a7b9028787903"),
    "title" : "Chemise bucheron, rouge/noir, a carreaux",
    "picture_url" : "https://www.projet13.com/9959-big_default/chemise-bucheron-rouge-noir-a-carreaux.jpg",
    "price" : NumberLong(4200),
    "redirect_url" : "https://www.projet13.com/6426-chemise-bucheron-rouge-noir-a-carreaux.html?gclid=Cj0KCQjwoaz3BRDnARIsAF1RfLfOJDZobj5gTT_uscskeyUqWgzZ_L2SJYvE_kMmk4weuzZpe-fc8cQaAr-IEALw_wcB#/10-taille-m/32-couleur-rouge"
}

```

Now, if we look at the forest admin part (model definitions), we have the following:

```javascript
// Outfit Document
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({ 
  'picture_url': String,
  'products': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],
}, {
  timestamps: false,
});

module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');

```

```javascript
// Outfit Product Document
// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
const mongoose = require('mongoose');

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = mongoose.Schema({ 
  'outfit_id': { type: mongoose.Schema.Types.ObjectId, ref: 'outfitsCollection' },
  'title': String,
  'price': Number,
  'redirect_url': String,
  'picture_url': String,
}, {
  timestamps: false,
});

module.exports = mongoose.model('outfitProductsCollection', schema, 'OutfitProductsCollection');

```

## Expected behavior

Now that I have my `belongsTo` and `hasMany` setup, I do have the following behavior (see below images)

> Outfit details

I have the details with a “link” to Products (which contains the related products)

 ![Screenshot 2020-06-19 at 12.11.13](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/7ea25ce5f132fb926a647a57b41f1b6d11cb4500.jpeg) ![Screenshot 2020-06-19 at 12.11.25](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/6bd0ccbfed5f77574bbe1b9275d93997a7a2c82d.png)

> Outfit Product details

I have the details of my product but not “link” to the related outfit

 ![Screenshot 2020-06-19 at 12.11.46](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/1bd8bb920db03099b2c071a5952ed3d0e064aa10.png)

## Actual behavior

I can’t retrieve the related document as it should.

Documentation links:

- HasMany → [https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-a-hasmany-relationship](https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-a-hasmany-relationship)
- BelongsTo → [https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-a-belongsto-relationship](https://docs.forestadmin.com/documentation/v/v6/reference-guide/relationships#adding-a-belongsto-relationship)

## Failure Logs

I have no failure logs at all.

## Context

Please provide any relevant information about your setup.

- Project Name: EyesApp

```javascript
"dependencies": {
    "chalk": "~1.1.3",
    "cookie-parser": "1.4.4",
    "cors": "2.8.5",
    "debug": "~4.0.1",
    "dotenv": "~6.1.0",
    "express": "~4.16.3",
    "express-jwt": "5.3.1",
    "forest-express-mongoose": "^6.0.0",
    "mongoose": "~5.8.2",
    "morgan": "1.9.1",
    "npm-watch": "^0.6.0",
    "require-all": "^3.0.0"
  }

```

We are currently plugged to MongoDB, no firebase implemented yet.

---

<div class="post-metadata">

**Author:** ![arnaud](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud/32/32_2.png) [@arnaud](https://community.forestadmin.com/u/arnaud)\
**Post date:** [June 22, 2020, 12:30pm UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/2 "2020-06-22T12:30:07Z")

</div>

Hi @Thomas

I’ll try to reproduce your issue today and will let you know if I find something.

---

<div class="post-metadata">

**Author:** ![arnaud](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/arnaud/32/32_2.png) [@arnaud](https://community.forestadmin.com/u/arnaud)\
**Post date:** [June 22, 2020, 1:22pm UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/3 "2020-06-22T13:22:12Z")

</div>

Unfortunately, I cannot manage to reproduce this issue.

I tried with this model:

```javascript
const schema = mongoose.Schema({
  'children': { type: [mongoose.Schema.Types.ObjectId], ref: 'persons' },
  'parent': { type: mongoose.Schema.Types.ObjectId, ref: 'persons' },
  'name': String,
}, {
  timestamps: false,
});

module.exports = mongoose.model('persons', schema, 'persons');

```

And everything is well set and displayed.

 ![Screenshot 2020-06-22 at 15.17.47](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/f2f76af54a98046857a4737830c66c242c9ee79a.png) ![Screenshot 2020-06-22 at 15.18.52](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/c57dc1333c9bbf408f16f7f61e77acca70ee86d5.png)

I am using the same version of `mongoose` and `forest-express-mongoose` as you do.  
What is the version of your Mongodb database?

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.forestadmin.com/u/Thomas)\
**Post date:** [June 22, 2020, 4:51pm UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/4 "2020-06-22T16:51:50Z")

</div>

Hi,

Thanks for your time, we are using MongoDB on Atlas on a Version 4.2.6 Cluster

Best,

---

<div class="post-metadata">

**Author:** ![anon34731316](https://avatars.discourse-cdn.com/v4/letter/a/eada6e/32.png) [@anon34731316](https://community.forestadmin.com/u/anon34731316)\
**Post date:** [June 23, 2020, 7:48am UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/5 "2020-06-23T07:48:09Z")

</div>

Hi Thomas,

I jumped to your usecase this morning and tried to reproduce your issue with no success.

However, I notice one little thing that could make the difference.  
Can you please try to replace, in your `outfits-collection` model, the key `products` by `products_ids`?  
Your model would be like this:

```javascript
const mongoose = require('mongoose');

const schema = mongoose.Schema({
  'picture_url': String,
  'products_ids': { type: [mongoose.Schema.Types.ObjectId], ref: 'outfitProductsCollection' },
}, {
  timestamps: false,
});

module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');

```

Let me know if that changes anything.  
Thanks!

PS: Really love the outfit example you gave us, the “Chemise bucheron” is our uniform 🧔 🌲 ⛏

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.forestadmin.com/u/Thomas)\
**Post date:** [July 3, 2020, 1:03pm UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/7 "2020-07-03T13:03:35Z")

</div>

Hey,

While checking, I found something weird

I had this line:

```javascript
'products': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],

```

However, you actually did something different:

```javascript
'products_ids': { type: [mongoose.Schema.Types.ObjectId], ref: 'outfitProductsCollection' },

```

> As you can notice, you did define {type: […]} and not [{type: …}]. As per the documentation → [https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-a-hasmany-relationship](https://docs.forestadmin.com/documentation/reference-guide/relationships#adding-a-hasmany-relationship) I did good?

But as usual… the link still don’t work (even if I was wrong because it was product\_ids and not products)

 ![Untitled3](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/3f76514aac0e36b16c4f85e6adee28a69bad802f.png)  
 ![Untitled4](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/99845102f116f9c929bc610c9ce3b4560abb418f.png)

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.forestadmin.com/u/Thomas)\
**Post date:** [July 3, 2020, 1:24pm UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/8 "2020-07-03T13:24:32Z")

</div>

Ok so, from the outfit, I can now finally access the related products!!

```javascript
const schema = mongoose.Schema({ 
  'picture_url': String,
  'products_ids': [{ type: mongoose.Schema.Types.ObjectId, ref: 'outfitProductsCollection' }],
}, {
  timestamps: false,
});

```

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/1X/e539c1f3a682d1c5443b6845fdc730dc40d7a156.png)

However, in the reversed way, I only get a string/data like this:

> Outfit id  
> \<client@model:forest-outfits-collection::ember776:5eff238d2935aec34c3b961e\>

---

<div class="post-metadata">

**Author:** ![Thomas](https://avatars.discourse-cdn.com/v4/letter/t/e36b37/32.png) [@Thomas](https://community.forestadmin.com/u/Thomas)\
**Post date:** [July 3, 2020, 1:53pm UTC](https://community.forestadmin.com/t/models-enrichment-relationship-doesnt-seem-to-work-as-expected/490/9 "2020-07-03T13:53:50Z")

</div>

Ok I don’t get it… I updated the models section

from:

```javascript
// outfits-collection.js
module.exports = mongoose.model('outfitsCollection', schema, 'OutfitsCollection');

// outfit-products-collection.js
module.exports = mongoose.model('outfitProductsCollection', schema, 'OutfitProductsCollection');

```

to:

```javascript
// outfits-collection.js
module.exports = mongoose.model('OutfitsCollection', schema, 'OutfitsCollection');

// outfit-products-collection.js
module.exports = mongoose.model('OutfitProductsCollection', schema, 'OutfitProductsCollection');

```

And now I do have the expected behavior, wut…
