Skip to content

virtual getter using pk is null on update #1735

Open

Description

@willmac321

When using a virtual column that modifies a primary key, if the primary key is not supplied on an update, the pk is null and the virtual column will try to operate on that null value. The value should always have a value?

Given a model like

UserModel {
   @PrimaryKey
   @Column({ type: Sequelize.BIGINT, autoIncrement: true })
   public id!: number;

   @Column({type:Sequelize.STRING})
    public comment!: string;

    @Column({
    type: Sequelize.VIRTUAL,
    get() {
      console.log(this);
      return thisFuncCantHaveNull(this.getDataValue('id'));
    },
}
///
const [affectedCount, rows] = UserModel.update({comment:'i like Mondays'}, {
        where: { id: suppliedId },
        transaction,
        returning: true,
});

then when the update is called on a record that exists already, the console.log of this will print something like:

UserModel {
  dataValues: {
    id: null,
    comment: 'i like Mondays',
  },
  _previousDataValues: {
   comment: null,
  },
  uniqno: 1,
  _changed: Set(1) { 'comment' },
  _options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
  isNewRecord: true
}

I think it makes sense that under the hood a new record is made and then updated with the existing record, however the null id is problematic since our virtual getter is trying to calculate a value on something that shouldn't be null... :(

Activity

  1. ccmetz commented on Aug 15, 2025

    @ccmetz

    Just ran into this same issue. The class properties are null when doing a model.update() and my virtual getter is throwing an error because its not expecting a certain property to be null.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions