Migration to `@forestadmin/agent` - Records search and filtering is broken with snake case fields

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

Records search and filtering is broken when we have fields in the db in snake_case , because the query tries to find the field in camelCase .

To fix this we’ve patched the broken module (@forestadmin/datasource-sequelize) and made this change to /dist/utils/query-converter.js:

This way it now works correctly:

Please, fix this on your side too.

Thank you,
Matteo

Here is the “meta” section of the forest admin schema file:

  {
    "liana": "forest-nodejs-agent",
    "liana_version": "1.41.7",
    "liana_features": null,
    "stack": {"engine": "nodejs", "engine_version": "20.11.1"}
  }

and from package.json:

"sequelize": "^6.28.0"

cc @julien.jolles

Hello @Matteo

I have tried to reproduce but I do not observe the same behaviour.

I have added a column “snake_case” in my table, the search and filters are working as expected.

The frontend does send the correct field name to my agent.
image

Since you are migrating from FES, I suppose you are using @forestadmin/datasource-sequelize, what version are you using, could you share the Sequelize Model of your Boat class ?

Thank you @dogan.ay

Please consider that we have snake_case in the db field names, and camelCase in our Sequelize models.

This is a field example in a model:

image (32)

Also consider that we’re setting up sequelize with options.define.underscored: true

About your question:

Since you are migrating from FES, I suppose you are using @forestadmin/datasource-sequelize, what version are you using

1.8.10

image (33)

Could you share the query that is being executed ? It is still working fine for me, even by setting up data that ressembles yours.

image

@dogan.ay we think the problem only arises with STRING fields.
No way to tell you the query, as the problems is raised while building the where clause, so no query is built… See the explanation below. We tried to debug the problem in your code. Follow along, please.

Inspecting the error in the log we started looking at the QueryConverter.getWhereFromConditionTree .

We’ve added a console.log just before the call to QueryConverter.makeWhereClause (next step in the stack trace).

and the error gets generated at the first field it finds with snake case:

image (51)

With these details we can know that the makeWhereClause is then calling QueryConverter.makeLikeWhereClause

At this point looking at makeLikeWhereClause we can see that it tries to get the safeField again.

Doing this it does the last jump of the stack trace to unAmbigousField , which having both isSafe and isRelation false executes the third branch of the if chain

image (54)

image (55)

between all the attributes returned from model.getAttributes() we see the shipyardOther one

image (56)

the problem is that since the safeField was passed at the beginning ('shipyard_other') instead of the model field ('shipyardOther'), then we have that model.getAttributes()[field]

is undefined (ofc) and that then causes the error.

This is the reason why in the patch we passed the field instead of the safeField.

So, because the error happens while generating the where clause, we can’t report here the query it does (because it doesn’t do any).

However, with the patch we suggested here’s the query (correct)

In your example everything works correctly because you’ve declared the idMachine as integer, so the where clause generated is a '=' , while if you try setting it as a string (or also setting lastName from your example, which I’m guessing already is a string) it should generate an 'ILIKE' and hence generate the error.

Hope this better explains the error, letting you reproduce on your side.

Thanks for the detailed report, I already had the problematic code in view but it was important to understand the cause of your issue as well as being able to reproduce it.

I have created a fix, I’ll let you know once it is merged :slight_smile:

1 Like

The fix has been released and is available with @forestadmin/datasource-sequelize v1.8.11

1 Like