sequelize where condition on 1 model have hasMany and belongsTo

Viewed 26

the error is simple

sqlMessage: "Unknown column 'dm_personal.full_name' in 'where clause'", but its only came when i put include that relation 2 model with belongsTo and hasMany. the hasMany is the problem here...

my engine:

    "sequelize": "^6.6.5",
    "sequelize-cli": "^6.2.0",

i have 1 model that looks like these

static associate(models) {
      // define association here
       models.form9.belongsTo(models.dm_personal, { foreignKey: 'dm_personal_id', constraint: false, });
       models.form9.hasMany(models.bongkar, {
        foreignKey: "form9_id",
        constraint: false
       });
      }

then when i call findAndCountAll

include: [
        {
          model: dm_personal, // this dm_personal is belongsTo
        },
        {
          model: bongkar, // here is problem make where keyword output : column not found 
// also i tried on mysql query its error is same. 
// when i comment this include , the query where /find its work like a charm.
// this bongkar is hasMany
        },

my query or search or where condition is look like these

[
  {
    '$dm_personal.full_name$': { [Symbol(substring)]: 'yogithesymbian' }
  }
] 

the code of keyword :

advanceSearchCondition.push({
            [`$${data[0]}$`]: { // here is : dm_personal.full_name // for ${data[0]}
              [Op.substring]: `${data[1]}` // here is : yogithesymbian  // for ${data[1]}
            } 
          });

here is my describe about data[i]

      let keywordSplit = keyword.split('&');
      keywordSplit.map(el => {
        const data = el.split("=");
...
...
so i split the query from frontend side...

my perform to load full code

await form9.findOne({
      include: [
        {
          model: dm_personal,
        },
        { // when i comment this bongkar , its work for search data
          model: bongkar,
          paranoid: false,
        },
      ]
      where: advanceSearchCondition,
      limit, offset,
      order: [
          ["id", `${sortInit}`],
      ],
})

by the way when i tried copied sql query from error and remove the search query its show all data with alias/as dm_personal.full_name its on there ,, but why the output is column not found ? .

or any solution that model still on include but ignore for the where keyword just only for has many include ? maybe any properties ? or hard code like if advanceSearch or any value of keyword is null use include 1 else include both them ( hasMany, belongsTo )

0 Answers
Related