Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

virtual getter using pk is null on update #1735

Open
willmac321 opened this issue Oct 16, 2024 · 0 comments
Open

virtual getter using pk is null on update #1735

willmac321 opened this issue Oct 16, 2024 · 0 comments

Comments

@willmac321
Copy link

willmac321 commented Oct 16, 2024

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... :(

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant