ErkkiBulldog
ErkkiBulldog

Reputation: 33

Including attribute from another table as one field with Sequelize

I am using Sequelize to include other model Importance to Event. Every event has importance value and importances are defined in different table. I need be able to ask events from backend so that it gives me importance value as one attribute. Currently it gives me object that includes attribute.

I have tried including model importance to Events.

Event.findAll({
      attributes: {
        exclude: ['importance_id']
      },
      include: [{
        model: Importance,
        attributes: ['value'],
      }]
    })

This is what I'm getting:

{
    "id": 1,
    "name": "name 1",
    "Importance": {
        "value": 1
    }
}

This is what it should look like:

{
    "id": 1,
    "name": "name 1",
    "value": 1
}

Upvotes: 2

Views: 2363

Answers (1)

ErkkiBulldog
ErkkiBulldog

Reputation: 33

I added raw: true inside findAll object and now I get data like this:

{
    "id": 1,
    "name": "name 1",
    "Importance.value": 1
}

This is not exactly what I wanted but it is ok workaround.

Upvotes: 1

Related Questions