I want to save without affecting UpdateDateColumn when updating view column.
This table is a post table and the view is a hit.
@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.
In TypeORM, you can add a created_at column and a updated_at column by making use of the CreateDateColumn and UpdateDateColumn decorators, respectively. These columns will be automatically initialized and updated to the current date and time (that is, the current timestamp).
Because of that, TypeoORM serializes them to strings, even though it seems to cause confusion. TypeORM works differently with the timestamp and timestamp with timezone columns. In contrast to the time and date columns, the timestamp data time contains everything needed to create a Date object.
The workaround is first do .findOne () and then do .save () if it didn't' find an account. Example is the second code block in the question above. If it can help people who face the same problem, i think that .save () will perform an update in case the primary column (id in this case) is precised and exists in database.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With