Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sequelize is returning null values when used with include

When querying a model with include, incase of no rows available, it is returning data with null values.

I know this problem has been posted many times before, but i have not found any conclusions till now.

Model.findOne({
    where: { id: 1 },
    include: [{model: Model1}]
})

Result i am getting is like

{
    "id": null,
    "name": null,
    "age": null,
    "model1": []
}

If i use raw: true, it is printing like

{
    "id": null,
    "name": null,
    "age": null,
    "model1.model1n": 0
}
like image 367
architjn Avatar asked Aug 29 '26 13:08

architjn


1 Answers

One cause of this is if you exclude id from attributes. If you're using scopes or attributes in your includes or through tables, make sure you're returning the id primary key.

Another cause of this is if you return null in a custom get() getter function on the column.

like image 134
sgarza62 Avatar answered Sep 01 '26 06:09

sgarza62



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!