In typeorm entity, I have this column:
@Column({
type: 'bigint',
nullable: true,
})
lockedTimestamp?: number;
And in my nest.js code I have this piece of code:
if (...) {entity.lockedTimestamp = null;}
Problem: My strict typescript is screaming that lockedTimestamp can't be null. Ts is telling me that it can be only undefined or a number.
What am I missing? Do I have correctly written a nullable column?
Will DB throw some error if I want to update it with undefined?
If you want to explicitly set it to null
, you will need to allow for null
on that property in your typing:
@Column({
type: 'bigint',
nullable: true,
})
lockedTimestamp?: number | null;
because as the documentation states, using the save function will skip the undefined properties:
save
- Saves a given entity or array of entities. If the entity already exist in the database, it is updated. If the entity does not exist in the database, it is inserted. It saves all given entities in a single transaction (in the case of entity, manager is not transactional). Also supports partial updating since all undefined properties are skipped. In order to make a valueNULL
, you must manually set the property to equalnull
.
So you would not get a DB error but the undefined
will be ignored by the save
function.