# Migration to @forestadmin/agent - problems with not null + default value fields

**URL:** <https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475>\
**Category:** Help me!\
**Created:** [September 25, 2024, 2:52pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475 "2024-09-25T14:52:51Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [September 25, 2024, 2:52pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/1 "2024-09-25T14:52:51Z")

</div>

Hi.  
We’re migrating from `forest-express-sequelize` to `@forestadmin/agent` .

We have a Sequelize model with a field defined as follows:

```js
  showCrew: {
    type: DataTypes.BOOLEAN,
    allowNull: false,
    defaultValue: true,
    field: 'show_crew',
  },

```

With the previous agent filed this used to be translated in the `.forestadmin-schema.json` file as:

```json
    {
      "field": "showCrew",
      "type": "Boolean",
      "defaultValue": true,
      "enums": null,
      "integration": null,
      "isFilterable": true,
      "isPrimaryKey": false,
      "isReadOnly": false,
      "isRequired": true, <<<<<<<<<<
      "isSortable": true,
      "isVirtual": false,
      "reference": null,
      "inverseOf": null,
      "validations": [{ <<<<<<<<<<
        "message": null,
        "type": "is present",
        "value": null
      }]
    }

```

With the _new_ agent, this is what we see in the `.forestadmin-schema.json` file for the very same field:

```json
    {
      "defaultValue": true,
      "enums": [],
      "field": "showCrew",
      "integration": null,
      "inverseOf": null,
      "isFilterable": true,
      "isPrimaryKey": false,
      "isReadOnly": false,
      "isRequired": false, <<<<<<<<<<
      "isSortable": true,
      "isVirtual": false,
      "reference": null,
      "type": "Boolean",
      "validations": [] <<<<<<<<<<
    },

```

The problem here is that with the new agent the _“not null”_ validation is lost (see the lines highlighted by arrows in the above code fragments).

As far as I can tell, with the new agent, whenever we have a field which is _not null with a default value_ in our models, this problem arises.

We’re not sure, but maybe this is due to this code in the agent:

> <https://github.com/ForestAdmin/agent-nodejs/blob/a8b56a59343445d9289b08f930baddf1283606dd/packages/datasource-sequelize/src/utils/model-to-collection-schema-converter.ts#L169-L177>

NB: this used to work correctly, instead, with the previous agent version.

May you please have a check?

Thank you.  
Matteo

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 26, 2024, 9:47am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/3 "2024-09-26T09:47:35Z")

</div>

Hello @Matteo

Thank for your report.  
As you pointed out, the new agent considers fields with default values as not required.

This shouldnt be an issue though, since either sequelize or your database knows how to fill in those values if missing, so from the _frontend/user_ perspective, those fields can be considered _not required_.

If you still feel like it is an issue, could you please share some details about your user’s workflow regarding this point?

Thanks,

@anon7311026

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [September 26, 2024, 1:18pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/4 "2024-09-26T13:18:58Z")

</div>

Hi @anon7311026  
Thank you for your reply.

Let me start by saying that passing to the new agent was expected to be seamless… That’s why, every time we encounter something different from the past, with the old agent, we ask. So thank you for your support.

In this particular case, the difference is that with the old agent, the user couldn’t select a not allowed value while editing an item (for example).  
Now with the new agent the user can. And after that, the user receives an error when the form is saved. I don’t know why this error arises: we have a default value in the model!  
But in the old agent version, thanks to the validation, we never hit this problem.

See this video: here we’re editing a record.  
In the first part of the video, you can see the behaviour with the old agent version. In the second part the new agent version behaviour is shown (as you can see the null option is allowd in the boolean).

![ezgif-4-3f7d8663e2](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/7/76a073745fdab17cfb183640c854263ac794078e.gif)

And this is the error shown by the new agent when the edit form is saved with the null value.

 ![Screenshot 2024-09-26 at 14.58.03](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/d/dfe5ac8129de00b9bb715e2ba6940a4f22805d10.png)

Thank you in advance for your feedback.  
Matteo

PS: since I wrote about this `listed` field, here is the field definition in the sequelize model:

```js
        listed: {
          type: DataTypes.BOOLEAN,
          allowNull: false,
          defaultValue: true,
        },

```

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [September 27, 2024, 9:05pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/5 "2024-09-27T21:05:05Z")

</div>

> [@Matteo](#):
>
> I don’t know why this error arises: we have a default value in the model!

Actually, I’m afraid I have an idea… 😇 please let me know if you think the following is correct 🙏

I think that this is Sequelize behaviour:

- **null** : Validation error because `allowNull` is `false`.
- **undefined** : The default value (`true`) is be used.

I think that choosing the `null` value in the boolean front-end widget, forces the field to be sent over as the `null` value. This can be easily seen in the developer tools of the browser: the `null` value is passed by forest in the payload.

But, in order to have the default value saved in the database by Sequelize, we should set somehow the value to `undefined`. But, AFAIK that’s not possible in the Forest frontend. We can only choose `true`, `false` or `null`.

Therefore, I think that this assumption is not correct:

> [@anon7311026](#):
>
> This shouldnt be an issue though, since either sequelize or your database knows how to fill in those values if missing, so from the _frontend/user_ perspective, those fields can be considered _not required_.

If we consider “not required” this field, then if the user leaves it unfilled, a `null` value is sent to the backend, finally reaching Sequelize, which does not trigger the default value. Because, in the Sequalize perspective, a value have actually been passed is: the `null` value.  
And `null !== undefined`: only `undefined` value is a “missing” value that triggers the default value in Sequelize.

Let me know your thoughts, please.

Thank you,  
Matteo

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [September 28, 2024, 6:17am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/6 "2024-09-28T06:17:36Z")

</div>

Hello. I think that your assumption is correct.

I will see how I can make a fix to improve this flow, either in the frontend or agent and let you know.

@anon7311026

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [September 29, 2024, 8:42am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/7 "2024-09-29T08:42:54Z")

</div>

Thank you @anon7311026  
If I may, I believe that the best and most suitable approach, in terms of seamless migration from old to new agent, would be to exactly replicate the previous behavior. 😅 🙏

Thank you very much,  
Matteo

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [October 2, 2024, 7:36am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/8 "2024-10-02T07:36:14Z")

</div>

Hi @anon7311026  
As you know we’re in the middle of a migration and this is the last issue we’re dealing with. Therefore, we would like to be in sync with you in order to be able to plan our production release.

Thus I was wondering if there are any updates about this.

Thank you for your feedback,  
Matteo

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [October 3, 2024, 7:21am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/9 "2024-10-03T07:21:32Z")

</div>

Hello @Matteo,

Sorry for the delay, we had a couple days off. [A fix](https://github.com/ForestAdmin/agent-nodejs/pull/1185) is almost ready on the agent side, but I will need a final round of internal review before we can move forward with it.  
Thanks for your patience.

---

<div class="post-metadata">

**Author:** ![Matteo](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/matteo/32/75_2.png) [@Matteo](https://community.forestadmin.com/u/Matteo)\
**Post date:** [October 8, 2024, 3:54pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/10 "2024-10-08T15:54:50Z")

</div>

Hi @anon7311026  
No problem.

In the mean time, has anything moved forward regarding this fix? 🙏

Thank you,  
Matteo

---

<div class="post-metadata">

**Author:** ![anon7311026](https://avatars.discourse-cdn.com/v4/letter/a/f9ae1b/32.png) [@anon7311026](https://community.forestadmin.com/u/anon7311026)\
**Post date:** [October 9, 2024, 11:55am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-problems-with-not-null-default-value-fields/7475/11 "2024-10-09T11:55:33Z")

</div>

Hello,  
The fix has been released to [@forestadmin/datasource-sequelize@1.10.3](https://github.com/ForestAdmin/agent-nodejs/releases/tag/%40forestadmin%2Fdatasource-sequelize%401.10.3)  
Please upgrade this dependency and let us know if it fixes your issue 🙏
