Cannot search on smart field with 2 joints following Woodshop

Hi,

I was doing a search on a smart field with 2 joints, it was working great before the update to v7.
Now, I keep having a log saying:

[...]
Cannot read property 'push' of undefined
[...]

So this part of the search is not working:

    search(query, search) {
      let split = search.split(' ');
      
      query.include.push({
          model: models.materials,
          as: 'idMaterial'
        });
        query.include.push({
          model: models.origins,
          as: 'idOrigin'
        });

I am currently working on a model called material_and_origin that is the link between the model origin and material (the correct relationships exist in their models).

So, two things:

  • How am I suppose to fix this failure ? I saw that it is linked to the declaration of Sequelize (cf this topic) but I don’t know how to change it since the declaration is now in index.js.
  • The documentation needs to be updated at the import level for the Op operator and maybe something else since I have the same structure and this is not working :grimacing:.

Context

  • Forest Express: 8.5.1
  • Express Version: 4.17
  • Sequelize Version: 7.9.1
  • Database Dialect: PostgreSQL
  • Database Version: 13

Thanks for your help :slight_smile:

Hey @LotusBlack :wave:

This was working on v6, right?
Could you try using and tell me if it’s working for your case ?

query.include = [{
  model: models.materials,
  as: 'idMaterial'
}, {
  model: models.origins,
  as: 'idOrigin'
}];
  • How am I suppose to fix this failure ? I saw that it is linked to the declaration of Sequelize (cf this topic ) but I don’t know how to change it since the declaration is now in index.js .

I’m not sure of what you mean here. models/index.js should export all the models you need, just like it did in v6. I would be happy to take a look at yours to check if everything is setup correctly.

  • The documentation needs to be updated at the import level for the Op operator and maybe something else since I have the same structure and this is not working

const Op = models.objectMapping.Op; should work as expected, but maybe you’ve encounter an issue between the time we updated the documentation and this thread, as this should have been fixed following your previous thread.

I can see that a fews changes were made on our end on this (The search function), so I’ll be happy to help if my proposed solution did not work on your case :slight_smile:

Hope that helps :pray:

1 Like

Hi @jeffladiray,

Thanks a lot for your answer.
This is not working, I have an error like:

[forest] 🌳🌳🌳  Unexpected error: missing FROM-clause entry for table "idMaterial"
{
  "name": "SequelizeDatabaseError",
  "parent": {
    "length": 122,
    "name": "error",
    "severity": "ERROR",
    "code": "42P01",
    "position": "275",
    "file": "parse_relation.c",
    "line": "3459",
    "routine": "errorMissingRTE",
    "sql": "SELECT \"id\", \"created_at\" AS \"createdAt\", \"updated_at\" AS \"updatedAt\", \"comment\", \"id_material\" AS \"idMaterialKey\", \"id_origin\" AS \"idOriginKey\" FROM \"public\".\"material_per_origin\" AS \"materialPerOrigin\" WHERE (lower(\"materialPerOrigin\".\"comment\")  LIKE  lower('%Glass%') OR \"idMaterial\".\"name\" ILIKE '%Glass%') LIMIT 10 OFFSET 0;"
[...]

Which is logical because when you look at the query :

SELECT "id", "created_at" AS "createdAt", "updated_at" AS "updatedAt", "comment", "id_material" AS "idMaterialKey", "id_origin" AS "idOriginKey" FROM "public"."material_per_origin" AS "materialPerOrigin" WHERE (lower("materialPerOrigin"."comment")  LIKE  lower('%Glass%') OR "idMaterial"."name" ILIKE '%Glass%') LIMIT 10 OFFSET 0

there is no mention of the table idMaterial in the from clause. So I suppose that the includes are not working.

const Op = models.objectMapping.Op; should work as expected, but maybe you’ve encounter an issue between the time we updated the documentation and this thread, as this should have been fixed following your previous thread.

Sorry I forgot to put a link, but I was talking about the Woodshop, about this page precisely Search on a smart field with two joints - Woodshop.

I’m not sure of what you mean here. models/index.js should export all the models you need, just like it did in v6. I would be happy to take a look at yours to check if everything is setup correctly.

Sure, you can find the file here:

const fs = require('fs');
const path = require('path');
const Sequelize = require('sequelize');

const databasesConfiguration = require('../config/databases');

const connections = {};
const db = {};

databasesConfiguration.forEach((databaseInfo) => {
  const connection = new Sequelize(databaseInfo.connection.url, databaseInfo.connection.options);
  connections[databaseInfo.name] = connection;

  const modelsDir = databaseInfo.modelsDir || path.join(__dirname, databaseInfo.name);
  fs
    .readdirSync(modelsDir)
    .filter((file) => file.indexOf('.') !== 0 && file !== 'index.js')
    .forEach((file) => {
      try {
        const model = connection.import(path.join(modelsDir, file));
        db[model.name] = model;
      } catch (error) {
        console.error('Model creation error: ' + error);
      }
    });
});

Object.keys(db).forEach((modelName) => {
  if ('associate' in db[modelName]) {
    db[modelName].associate(db);
  }
});

db.objectMapping = Sequelize;
db.connections = connections;

module.exports = db;

Thanks for your help @jeffladiray, always appreciated!

Hey @LotusBlack :wave:

Would you mind trying with forest-express-sequelize@7.9.4 ? We’ve just release a fix for this issue which looks pretty similar to yours. I’ve been able to reproduce the problem detailled there and this version should hopefully fix your issue as well.

I will take a look at the woodshop example you mentionned, just to be sure the sample code is still valid for v7.

(BTW, your models/index.js looks perfectly fine to me :+1:)

Let me know if that’s the case or not :raised_hands:

1 Like

Hey @jeffladiray,

Thanks a lot this is working!

Have a nice day.

1 Like