# Unnecessary Smart Field Injection?

**URL:** <https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070>\
**Category:** Help me!\
**Created:** [March 10, 2021, 4:52pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070 "2021-03-10T16:52:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 10, 2021, 4:52pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/1 "2021-03-10T16:52:16Z")

</div>

We have a Smart Field that performs a relatively expensive, asynchronous operation inside its getter.

When accessing a Collection that is related to the Collection that contains the Smart Field, even though the Smart Field is not present in any Summary or Detail views, the getter for the Smart Field is accessed. It looks like `forest-express`'s `smart-field-values-injector` is the reason for the call to the getter.

Is there a way to prevent Smart Field injection when it’s not accessed by any of the current views in the FA UI?

Thanks!

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [March 11, 2021, 9:06am UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/2 "2021-03-11T09:06:50Z")

</div>

Hi @yeti182,

Could you please provide those informations so I can help you 🙂 :

- What package are you using forest-express-mongoose or forest-express-sequelize ?
- What is the version of this package ?
- What is your project name ?
- What’s the name of your smart field ?

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 11, 2021, 1:57pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/3 "2021-03-11T13:57:34Z")

</div>

Hi @anon62739609 - we’re using forest-express-sequelize @ 7.0.0. I’ll message you with other details. Thanks!

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [March 11, 2021, 2:33pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/4 "2021-03-11T14:33:26Z")

</div>

Okey weird, this is something we fixed not a long time ago 🤔.

- It’s happening only in record details ?
- Could you please share the query parameters sent to the request replying with those smart fields 🤔 ?

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 11, 2021, 2:46pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/5 "2021-03-11T14:46:53Z")

</div>

Yes, it only happens when we access a related Collection’s record details, as far as I can tell.

In our case, it happens immediately after (and before subsequent calls to the admin API):

`OPTIONS /forest/User/{UserID}`

---

<div class="post-metadata">

**Author:** ![anon62739609](https://avatars.discourse-cdn.com/v4/letter/a/c2a13f/32.png) [@anon62739609](https://community.forestadmin.com/u/anon62739609)\
**Post date:** [March 11, 2021, 3:59pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/6 "2021-03-11T15:59:30Z")

</div>

I need the `GET /forest/User/{UserID}`, at least the query parameters

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 11, 2021, 5:45pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/7 "2021-03-11T17:45:44Z")

</div>

No problem. Just messaged you the details! Thanks, @anon62739609.

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [March 12, 2021, 1:42pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/8 "2021-03-12T13:42:34Z")

</div>

Hello @yeti182

Thank you for attracting our attention to the issue.

Unfortunately, we are currently not providing any configuration for retrieved fields.

I pushed your insight to our product team via our [ProductBoard](https://portal.productboard.com/forestadmin/1-forest-admin-product-roadmap). They will look into it. I can’t give you an ETA for this feature to be implemented but be assured that we all work hard to deliver new features as fast as possible.

Have a nice day, and please let me know if I can help you in any other way 🙂

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 12, 2021, 1:45pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/9 "2021-03-12T13:45:30Z")

</div>

Hi @anon90145840 - thanks for looking at this. Hmmm, I’m not looking for any configuration. Is it possible there’s a bug that’s causing the field to be retrieved even though it’s not currently being accessed in the FA UI?

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [March 12, 2021, 1:47pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/10 "2021-03-12T13:47:54Z")

</div>

It’s not a bug. Currently, when accessing the details of the records all the fields are systematically calculated.

I pushed the request to change this implementation to only request from the backend the fields that are currently used in the UI.

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 12, 2021, 1:49pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/11 "2021-03-12T13:49:59Z")

</div>

Ah - that’s not quite what’s going on, here. Let’s say I’m accessing a “User.” The FA UI is causing the getter of a “Books” SmartField (a Collection related to Users but not shown anywhere in the UI) to be called.

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [March 12, 2021, 1:59pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/12 "2021-03-12T13:59:30Z")

</div>

Okay, I see.

@anon62739609 shared with me the info you sent him.

I do not see any `GET` on `/relationship` route for users with the collection (let’s call it collection X) you mentioned to contain the smart field you are talking about. Does the collection X containing the smart field has a relationship with `Users` collection?

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 12, 2021, 2:09pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/13 "2021-03-12T14:09:18Z")

</div>

Collection X does not have a relationship with the `Users` collection. Thanks so much for looking at this.

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [March 12, 2021, 2:12pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/14 "2021-03-12T14:12:27Z")

</div>

Are you sure the smart action getter is called when you access the `User` records details?

Is it some specific User or all of them?

Does some of the related to `User` collections have a reference to collection X?

I am sorry to bother you wil all those questions, I am just trying to understand what is going on

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 12, 2021, 2:38pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/15 "2021-03-12T14:38:14Z")

</div>

No problem - happy to answer.

Yes, I am confident it’s called when accessing `User` record details. It occurs for all Users.

Yes, the `User` collection is related to Collection Y, which is related to Collection X.

---

<div class="post-metadata">

**Author:** ![anon90145840](https://avatars.discourse-cdn.com/v4/letter/a/ed8c4c/32.png) [@anon90145840](https://community.forestadmin.com/u/anon90145840)\
**Post date:** [March 12, 2021, 2:47pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/16 "2021-03-12T14:47:58Z")

</div>

Could you give me the name of the Y collection?

And also please copy the `GET` request on the `/relationships/` route for collection Y when you access the `User` one?

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 12, 2021, 2:52pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/17 "2021-03-12T14:52:38Z")

</div>

Sure thing - I’ll message you the names and fresh logs for the GET requests.

Upon examination, `Users` is actually related to Collection Y, which is related to Collection Z, which is related to Collection X. Onion skins!

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [March 16, 2021, 9:44am UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/18 "2021-03-16T09:44:50Z")

</div>

Hi @yeti182,

Just to check I got everything right, let me sum this up:

- You defined a smart field, performing expensive and async operation on a collection (Z)
- This collection is related to `User` via X/Y/Z collections
- Rendering a user trigger the smart field computation on collection Z, causing the issue.

I looked into this, but I’m not able to reproduce the behavior you are having. I did 2 tests this morning:

- A table `User` related to `Project` via an association table (UserProject). Project has a smart field named `logger`
- A table `User` related to `Project` via an association table (UserProject) related to `Organizations` via an association table (ProjectOrganization). Organizations has a smart field named `logger`

In both of those cases, rendering the User table view/summary view/details view did not trigger any smart field computation.

If possible, would you mind sharing (In private if not possible here) some more in-depth structure/table relation/definitions for all these relations between X/Y/Z? Being able to reproduce the issue on my end would help 🙏

---

<div class="post-metadata">

**Author:** ![yeti182](https://avatars.discourse-cdn.com/v4/letter/y/858c86/32.png) [@yeti182](https://community.forestadmin.com/u/yeti182)\
**Post date:** [March 16, 2021, 12:36pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/19 "2021-03-16T12:36:31Z")

</div>

Hi @anon39848301 - I’m happy to provide in-depth structure and definitions. I’ll message you now.

---

<div class="post-metadata">

**Author:** ![anon39848301](https://avatars.discourse-cdn.com/v4/letter/a/b9e5f3/32.png) [@anon39848301](https://community.forestadmin.com/u/anon39848301)\
**Post date:** [March 16, 2021, 3:44pm UTC](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070/20 "2021-03-16T15:44:47Z")

</div>

To sum up what have been discussed:

The 2 collections causing issues here are related with a smart belongsTo relationship. On FA, `belongsTo` relationships are retrieved at the same time the main datas are.  
Eg. `User` belongs to `Project` via smart relationship.  
`GET forest/user/someId` will include the associated `Project` definition.

In this case, `Project` has some expensive & asynchronous smart field computation (Let call it `expensiveSmartFieldComputation`), causing the UI to feel laggy when displaying a User even if the `expensiveSmartFieldComputation` field is not displayed nor used in the page.

Sadly, it is not currently possible to choose if this smart field (from a smart relationship), needs to be computed or not, so I’m pushing this to our productboard.

On the other side, here are a few possibilities to handle such cases:

- Replace the smart relationship by a SQL one - Normally the smart field should not be computed if not required in such a case.
- Compute the value when needed, via a smart action for example.

[Next page](https://community.forestadmin.com/t/unnecessary-smart-field-injection/2070.md?page=2)
