How to set a nullable database field to NULL with typeorm? How to set a nullable database field to NULL with typeorm? express express

How to set a nullable database field to NULL with typeorm?


After a good night rest I managed to solve my problem.

Typeorm sets the type of the database fields based on the typing you give the variables for your entities in typescript. Typeorm casts the code below to a varchar field in my postgres database because I gave it a string as a type in typescript.

@Column({    unique: true,    nullable: true,})resetPasswordToken!: string;

This is also where lies my problem. Typeorm takes the typing of a field and tries to create that database field based on the typing it reads. While the code below is correct, typescript basically encapsulates both types in a single object and that object is what is being read by Typeorm causing the error that I got.

resetPasswordToken!: string | null;

To fix my problem I had to specifiy the database field type explicitly like this:

@Column({    type: 'text',    unique: true,    nullable: true,})resetPasswordToken!: string;