Currently, I am using
@nestjs/typeorm": "^8.0.2
in conjunction with Postgres and encountering an unusual issue that seems like unexpected behavior:
When attempting to save a partial entity, the fields I specify are saved correctly, but the resulting value includes additional fields with null values. This makes it challenging for me to rely on the returned object as it now contains incorrect data.
As an example (in a simplified form), let's consider a Project Entity:
class Project extends BaseEntity {
@PrimaryGeneratedColumn()
id: number;
@Column({ length: 150 })
name: string;
@Column({ nullable: true })
data?: string;
}
My goal is to update only the name field, so suppose I have this function:
async saveName(proj: {id: number, name: string}): Promise<Partial<IProject>> {
... [some code]...
return await entityManager.save(Project, proj);
}
Now, if the entry in the database is:
{id: 1, name: 'a', data: 'my data' }
and I call:
await saveName({id: 1, name: 'b'})
The resulting value will be:
{id: 1, name: 'b', data: NULL }
I would expect the result to be:
{id: 1, name: 'b'}
or:
{id: 1, name: 'b', data: 'my data' }
(less ideal, but at least accurate)
In this context, please understand:
- This scenario is crucial for the nature of my project.
- I cannot always anticipate the fields to update; I need to modify the received object directly.
To summarize, the returned value is flawed.
Could this be a bug?
Is there any workaround available?