unflores
unflores

Reputation: 1810

Sequelize update not accepting `null` as an attribute for typescript

I am getting a weird error with the following db request:

RequestApp.update({
    is_selected: false
  }, {
    transaction: dbTransaction,
    where: {
      is_selected: null,
      request_id: request.id,
      user_id: {
        [db.Sequelize.Op.ne]: id
      }
    }
  })

The error I get is the following:

Error TS2322: Type 'null' is not assignable to type 'Object | WhereOptions[]'.

Technically I can get rid of the error by setting null as any, but it seems like there should be a way to set it directly to the where clause.

Is there a way to do this without explicitly overriding the data value?

Upvotes: 5

Views: 2089

Answers (2)

Reculos Gerbi Neto
Reculos Gerbi Neto

Reputation: 449

The attribute of your model definition must be "type | null" and you need to inform Sequelize manually that your field is type number and the right operator to check if is null is Op.is.

Example:

Model definition:

class MyModel {
  @Column(DataType.NUMBER)
  score: number | null;
}

Query:

MyModel.findAll({ where: { score: { [Op.is]: null } } });

Upvotes: 5

Ishmeet Singh
Ishmeet Singh

Reputation: 1246

Could use:

is_selected: { [Op.eq]: null }

Upvotes: -1

Related Questions