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

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

I think we spot some additional bug on your side :bug: 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 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:

Why does this happen? Here is what we understood…

This is our virtual field defined in the sequelize model:

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…):

And this is the console log result:

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

which logs:

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

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 :pray:

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”:

The same in the settings: no isValid field:

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

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

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 :confused: ?

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

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 :confused: ?

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).

And a 400 error is received:

Thank you :pray:
Matteo

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

1 Like

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

1 Like

Hello,

Could you please pull the latest version of the datasource-sequelize package? :pray:

Let me know if everything works fine after the update.

Alban