Skip to content

Cannot set embedded entity to null #3913

@adamalfredsson

Description

@adamalfredsson

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

Database system/driver:

[ ] cordova
[ ] mongodb
[ ] mssql
[ ] mysql / mariadb
[ ] oracle
[x] postgres
[ ] cockroachdb
[ ] sqlite
[ ] sqljs
[ ] react-native
[ ] expo

TypeORM version:

[x] latest
[ ] @next
[ ] 0.x.x (or put your version here)

Steps to reproduce or a small repository showing the problem:

Check out this small repository: https://github.com/nomadoda/typeorm-embedded-bug

I have an embedded entity:

export class Salary {
  @Column("decimal", { nullable: true })
  public amount: number;
}

And I'm using it in this entity:

@Entity()
export class Employee extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column(() => Salary)
  salary?: Salary | null;
}

Consider this:

createConnection()
  .then(async connection => {
    await connection.synchronize(true);
    const salary = new Salary();
    salary.amount = 120;
    const employee = Employee.create({
      salary
    });
    await employee.save();
    console.log("Saved a new employee with id: " + employee.id);

    console.log("Set salary to null and save");
    employee.salary = null;
    await employee.save();

    console.log("Get employee");
    await employee.reload();
    console.log(employee.salary);
    // Salary { amount: '120' }
  })
  .catch(error => console.log(error));

Expected outcome

Setting salary to null should set all embedded fields in database to null.

Actual outcome

Salary did not change on update

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions