hasMany returning a object with null fields when no data on the related table
See original GitHub issueWhat 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:
- Created 7 years ago
- Comments:7 (1 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I’ve got the same problem. I deliberately removed
id
from model usingModel.removeAttribute('id')
. I don’t need any primary key for that model. Why removing the primary keyid
causing this issue ?If someone still interested in this problem, add “where” in “include”, but in same level as “through” in findAll options: