typeorm

TypeORM When updating a specific column, save without affecting UpdateDateColumn


I want to save without affecting UpdateDateColumn when updating view column.

This table is a post table and the view is a hit.


UpdateDateColumn is updated when adding the number of view Column.
I don't want the view column to affect UpdateDateColumn.
@Entity("post")
export default class Post extends BaseEntity {
  @PrimaryGeneratedColumn()
  idx: number;

  // Skip
  
  @Column({
    nullable: false,
    default: 0
  })
  view: number;

  @Column("timestampz")
  @CreateDateColumn()
  created_at: Date;

  @Column("timestampz")
  @UpdateDateColumn()
  updated_at: Date;
}

Give me a solution please.


Solution

  • Not a perfect solution, but still a solution...

    You can force update your date column with the value it has:

    getRepository(Post).update(postId, {
      view: newViewValue,
      updated_at: () => '"updated_at"'
    })
    

    The SQL-query will look like:

    UPDATE "post" SET "view" = $2, "updated_at" = "updated_at" WHERE "idx" = $1