# Migration to @forestadmin/agent - can't create records if sequelize model has virtual fields

**URL:** <https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661>\
**Category:** Help me!\
**Created:** [November 26, 2024, 2:44pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661 "2024-11-26T14:44:23Z")\
**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:** [November 26, 2024, 2:44pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/1 "2024-11-26T14:44:23Z")

</div>

As you know we’ve migrated from `forest-express-sequelize` to `@forestadmin/agent`.

I think we spot some additional bug on your side 🐛 let me explain the thing.

We have a sequelize model called `Coupon` with a number of fields. And we also have one (or more) [sequelize virtual fields](https://sequelize.org/api/v6/class/src/data-types.js~virtual) in this model.

In forest this model is a collection.  
And this collection has the built in “create” new record feature.  
This correctly used to work with the old `forest-express-sequelize` agent.

With the new agent `@forestadmin/agent` this throws an error:

 ![image__75_](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/f/f3ecf9e394c0a1edb626b133f58e4b2799b72b6c.png)

Why does this happen? Here is what we understood…

This is _our_ virtual field defined in the sequelize model:

 ![image__80_](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/b6252f7ce7a990e44815dbf16f64f29cbb980ab1.png)

And we did _NOT_ add this virtual field in forest (as a “smart” field).

In your lib in `@forestadmin/datasource-customizer` in `BinaryCollectionDecorator.convertRecord` method you do this (we added a console log…):

 ![image (81)](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/8/8580513afe56f19048b321f610b9241bd5dd504c.png)

And this is the console log result:

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

Then `@forestadmin/datasource-toolkit` in `SchemaUtils.getField` is called for each record field:

 ![image (82)](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/0/09f2dc8fb0fd3b1d76543cb47a430d7605e27382.png)

which logs:

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

The above comes from the `.forestadmin-schema.json`, which _does not contain_ the `isValid` sequelize virtual field.

Therefore, an error is raised, up to the forest interface. Because you are doing an `Object.fromEntries` upon the sequelize fields (including virtual fields). But in the forest collection fields you don’t have the virtual fields, so this throws an error with the virtual fields, that you should skip.

Thank you for your support,  
Matteo

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [November 26, 2024, 4:10pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/2 "2024-11-26T16:10:36Z")

</div>

Hello Matteo,

It seems we might not be supporting virtual fields from Sequelize, which could explain why the field isn’t included in the Forest Admin schema. When you start the server, do you see a warning log like this: _“Skipping column 'coupon.isValid …”_?

That said, it’s a bit odd because the `isValid` field appears to be present when a create action is triggered. Are you perhaps using an older layout built with a different agent?

If you want to integrate your `isValid` field, you can probably duplicate or re-use your “get” definition to our `addField` method to create a forest virtual field.

Best regards 🙏

---

<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:** [November 26, 2024, 5:25pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/3 "2024-11-26T17:25:22Z")

</div>

Hi @Alban_Bertolini

My answers below. Thank you for your support.

> When you start the server, do you see a warning log like this: _“Skipping column 'coupon.isValid …”_ ?

Yes:  
`Skipping column 'Coupon.isValid' (Unsupported data type: "VIRTUAL")`

> it’s a bit odd because the `isValid` field appears to be present when a create action is triggered.

There’s not the isValid field in the create form (it’s not hidden, it’s just not there!). I doublechecked by editing the layout of the “create”:

 ![Screenshot_2024-11-26_at_18_14_47](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/2/233caa089f6c39dd038ca4c794d0c0c86614d6d6.png)

The same in the settings: no `isValid` field:

 ![Screenshot_2024-11-26_at_18_17_48](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/e/e1e7274147290e14ff5d6d5e8817076d41318bf9.png)

I can send you the screenshots privately if you need. I’m undisclosing the field names here 🙏

> Are you perhaps using an older layout built with a different agent?

Yes, of course! We’ve recently migrated from `forest-express-sequelize` to `@forestadmin/agent` thus all the layouts have been created in the past, with the old agent.

> If you want to integrate your `isValid` field, you can probably duplicate or re-use your “get” definition to our `addField` method to create a forest virtual field.

No I don’t want to integrate this field. It’s not exported to Forest and we’re ok with this.  
We just need to be able to create a new Coupon…

My question is: why you find this field when you iterate though the record fields with `Object.entries` in `@forestadmin/datasource-customizer` in `BinaryCollectionDecorator.convertRecord` method?

Thank you,  
Matteo

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [November 27, 2024, 7:34am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/4 "2024-11-27T07:34:58Z")

</div>

Thank you for your quick and clear answer.

> why you find this field when you iterate though the record fields with `Object.entries` in `@forestadmin/datasource-customizer` in `BinaryCollectionDecorator.convertRecord` method?

Before to try to reproduce the bug on my end, can you inspect the http request sent when you try to create the _Coupon_ from your forestadmin and check if there is the _isValid_ field please because I suspect an issue from the front 😕 ?

---

<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:** [December 2, 2024, 11:28am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/5 "2024-12-02T11:28:09Z")

</div>

Hi @Alban_Bertolini  
I apologise for my late answer. We had an internal event and was fully absorbed by it 🙏

> Before to try to reproduce the bug on my end, can you inspect the http request sent when you try to create the _Coupon_ from your forestadmin and check if there is the _isValid_ field please because I suspect an issue from the front 😕 ?

By inspecting the payload of the request sent from the browser I don’t see the `isValid` field.  
I can only see the fields I filled in (I only filled in the mandatory fields).

 ![Screenshot 2024-12-02 at 12.17.24](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/95879fef1848c40ff08a442dbe84f9566402fa4f.png)

And a `400` error is received:

 ![Screenshot_2024-12-02_at_12_21_16](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/9/92041a5bb97f351ae945368c224238b344cf085f.png)

 ![Screenshot_2024-12-02_at_12_23_41](https://europe1.discourse-cdn.com/flex013/uploads/forest/original/2X/b/b10eeb6d459357390cc8d1710b09f9a7e1119e84.png)

Thank you 🙏  
Matteo

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [December 2, 2024, 12:24pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/6 "2024-12-02T12:24:04Z")

</div>

Ok, thanks for your answer! 🙏  
I’ll look into it right away  
edit: I can reproduce. I’m trying to fix the bug.

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [December 2, 2024, 3:56pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/7 "2024-12-02T15:56:01Z")

</div>

The fix is in review, I hope it will merged tomorrow.

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [December 3, 2024, 4:01pm UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/8 "2024-12-03T16:01:51Z")

</div>

Hello,

Could you please pull the latest version of the `datasource-sequelize` package? 🙏

Let me know if everything works fine after the update.

Alban

---

<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:** [December 6, 2024, 9:52am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/9 "2024-12-06T09:52:37Z")

</div>

Thank you @Alban_Bertolini  
Now it works correctly 👍

Bye,  
Matteo

---

<div class="post-metadata">

**Author:** ![Alban\_Bertolini](https://dub1.discourse-cdn.com/flex013/user_avatar/community.forestadmin.com/alban_bertolini/32/1077_2.png) [@Alban\_Bertolini](https://community.forestadmin.com/u/Alban_Bertolini)\
**Post date:** [December 6, 2024, 11:00am UTC](https://community.forestadmin.com/t/migration-to-forestadmin-agent-cant-create-records-if-sequelize-model-has-virtual-fields/7661/10 "2024-12-06T11:00:02Z")

</div>

That’s great to hear! I’m happy to help you. 😊
