简体   繁体   中英

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM