question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

hasMany returning a object with null fields when no data on the related table

See original GitHub issue

What you are doing?

When doing a query that include a relation to another table (hasMany) when there is no data in the related table return an array with a object with all the fields null.


Product = sequalize.define('product', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true,
    },
    name: {
      type: DataTypes.STRING,
      validate: {
        notNull: true,
        notEmpty: true,
      },
    },
    state: {
      type: DataTypes.ENUM('published', 'pending', 'draft', 'deleted'),
    }
});

ProductFeature = sequalize.define('product_feature', {
    name: {
      type: DataTypes.STRING,
    },
    description: {
      type: DataTypes.STRING,
    }
});

Product.hasMany(ProductFeature,
    {
      foreignKey: 'id_product',
      as: 'features',
    }
  );


// In another File
Product.findAll({
    where: {
      state: 'published',
    },
    order: [
      ['creation_date', 'DESC'],
    ],
    include: [
      {
        model: ProductFeature,
        as: 'features',
      }, // load all pictures
    ],
  })

What do you expect to happen?

Return an array of products, each with an array of features

What is actually happening?

The array of product is returned but when the product dont have feature the features field is an array with one eleiemet with null on all the fields of the feature

"features": [
      {
        "name": null,
        "description": null,
        "id_product": null
      }
    ]

Output, either JSON or SQL

Dialect: Mysql Database version: mysql Ver 14.14 Distrib 5.7.12, for Linux (x86_64) Sequelize version: 3.23.6

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
ratulSharkercommented, Apr 16, 2020

I’ve got the same problem. I deliberately removed id from model using Model.removeAttribute('id'). I don’t need any primary key for that model. Why removing the primary key id causing this issue ?

0reactions
dr-miraclecommented, Jun 28, 2021

If someone still interested in this problem, add “where” in “include”, but in same level as “through” in findAll options:

const fooSample = await Foo.findAll({
    include: [{
        model: Bar,
        where: {},
        through: {
            where: {
                //your search params
            }
        },
    }]
});
Read more comments on GitHub >

github_iconTop Results From Across the Web

Laravel : HasMany relationship returns null data when added ...
I am facing a problem to return a selected column data using hasMany relationship. I've two tables loadbooking , loadbooking_packages .
Read more >
Relations return null - Laracasts
In this model I have two relations. A belongsTo relation for the user object that created the node and a hasOne relation to...
Read more >
Working with Databases: Active Record - Yii Framework
An Active Record class is associated with a database table, an Active ... will return the related Active Record instance or null if...
Read more >
Associations - Sequelize
The BelongsTo association; The HasMany association; The BelongsToMany association. The guide will start explaining how to define these four ...
Read more >
Relation queries (Concepts) - Prisma
Nested reads allow you to read related data from multiple tables in your database - such as a ... posts: true, // Include...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found