# How to set foreign key with a smart collection

**URL:** <https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556>\
**Category:** Help me!\
**Tags:** howtos\
**Created:** [August 10, 2023, 2:47pm UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556 "2023-08-10T14:47:12Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adel\_de\_Clevermate](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/adel_de_clevermate/32/4608_2.png) [@Adel\_de\_Clevermate](https://community.forestadmin.com/u/Adel_de_Clevermate)\
**Post date:** [August 10, 2023, 2:47pm UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556/1 "2023-08-10T14:47:12Z")

</div>

## Context

I have a smart collection “Contact” (contacts are section of table Users that have status=client).  
Other table “Besoins” that has a foreign key user\_id that refers to the collection Users.

Here is a screenshot of collection Besoins:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/bd5e3a79fbff4e66840cf315a92802fbfc5dbc8f.png)  
When I click on the contact link it goes to Collection Users, however I want that this link goes to smart collection Contact.

This is the relationship in the Besoin Model:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/706add805f7576893c8faa691179a51e83822866.png)

I dont have a Model Contact since it’s a smart collection, I got only forest/contact.js and routes/contact.js.

## Feature(s) impacted

Link between two collections

## Observed behavior

link goes to collection “Users” instead of smart collection “Contact”

## Expected behavior

Link should be to the smart collection “Contact”

- Project name: Clevermate
- Environment name: Test
- Database type: POSTGRES

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [August 10, 2023, 2:57pm UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556/2 "2023-08-10T14:57:00Z")

</div>

Hello @Adel_de_Clevermate,

I think it should be possible by creating a new smart field referencing your contact collection.

We cannot change the model and the relationship between `Besoins` and `Users` as it is defined like this in your model.

But it’s possible to create a new “smart” field that will add this “fake” relationship.

You can find some documentation about [smart relationships in the documentation](https://docs.forestadmin.com/documentation/reference-guide/models/relationships/create-a-smart-relationship). Let me know if it solves your issue.

---

<div class="post-metadata">

**Author:** ![Adel\_de\_Clevermate](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/adel_de_clevermate/32/4608_2.png) [@Adel\_de\_Clevermate](https://community.forestadmin.com/u/Adel_de_Clevermate)\
**Post date:** [August 10, 2023, 6:42pm UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556/4 "2023-08-10T18:42:24Z")

</div>

Hi @GuillaumeGautreau

This helps so far, thank you!

Following the Doc, I implemented a new smart field (contact\_ref) to add a relationship, it gets the contact as you see here

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/d/d5eeeb48aa4d96962f4572bb3e4b25090fabcbec.png)  
But it is not shown as a **Link** as expected.

this the the smart field:

 ![image](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/5/526b2b1a50c9920290e098a9e1abab0fee0759e4.png)

Do you have any idea why this happens, please?

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [August 11, 2023, 8:17am UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556/5 "2023-08-11T08:17:35Z")

</div>

Hello @Adel_de_Clevermate,

I reproduced your use case locally with a different DB, and I could manage to have a link between the 2.

I experienced an issue when the field name contained the character `_`. Could you rename your field `contactRef` and see if it works?

If it fixes your issue, I also suggest replacing the getter by:

```js
collection("cBesoins", {
  fields: [{
    field: 'contactRef',
    type: 'String',
    reference: 'contacts.id',
    get: (besoin) => besoin.user
  }]
});

```

As there is a relationship between `besoin` and `user`, the linked `user` should have already been loaded.

---

<div class="post-metadata">

**Author:** ![GuillaumeGautreau](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/guillaumegautreau/32/557_2.png) [@GuillaumeGautreau](https://community.forestadmin.com/u/GuillaumeGautreau)\
**Post date:** [August 11, 2023, 8:23am UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556/6 "2023-08-11T08:23:14Z")

</div>

For your information, we released a new agent package named `@forestadmin/agent`, along with `@forestadmin/datasource-sequelize` that has a more robust and simpler coding interface.

We strongly advise people switching to this new agent because it will replace the `forest-express` based solution in the long term.

---

<div class="post-metadata">

**Author:** ![Adel\_de\_Clevermate](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/adel_de_clevermate/32/4608_2.png) [@Adel\_de\_Clevermate](https://community.forestadmin.com/u/Adel_de_Clevermate)\
**Post date:** [August 11, 2023, 5:49pm UTC](https://community.forestadmin.com/t/how-to-set-foreign-key-with-a-smart-collection/6556/7 "2023-08-11T17:49:18Z")

</div>

> [@GuillaumeGautreau](#):
>
> `contactRef`

Thanks @GuillaumeGautreau !  
It worked for me when I replaces contact\_ref by contactRef.
