@OneToMany cascade not working as expected!
sapabg opened this issue · comments
Is there an existing issue for this?
- I have searched the existing issues
Current behavior
I have 2 Entities:
@Entity('sales')
export class Sale {
@PrimaryGeneratedColumn({name: "id", type: "int"})
id: number;
@OneToMany(type => SalePayment, sale_payments => sale_payments.sale)
sale_payments: SalePayment[];
@OneToMany(type => SaleDetail, sale_details => sale_details.sale, {
cascade: ["insert"],
// persistence: false
})
sale_details: SaleDetail[];
}
@Entity('sale_details')
export class SaleDetail {
@PrimaryGeneratedColumn({name: "id", type: "int"})
id: number;
@ManyToOne(type => Sale, sale => sale.sale_details)
@JoinColumn({ name: "sale_id", referencedColumnName: 'id' })
sale: Sale;
@Column()
sale_id: number;
@Column()
name: string;
}
When I try to insert a Sale with Sale Details like this:
const sale = new Sale();
sale.id = body.id; // body is request body and it is present
sale.open_date = moment(body.open_date, 'DD.MM.YYYY').format('YYYY-MM-DD');
sale.open_time = body.open_time;
const detail1 = new SaleDetail();
detail1.name = 'Name1';
const detail2 = new SaleDetail();
detail2.name = 'Name2';
sale.sale_details = [detail1, detail2];
this.connection
.manager
.save(sale);
This is all in a service with:
constructor(private connection: Connection) {}
This is what I get:
query: SELECT "Sale"."id" AS "Sale_id", "Sale"."open_date" AS "Sale_open_date", "Sale"."open_time" AS "Sale_open_time", "Sale"."total_without_vat" AS "Sale_total_without_vat", "Sale"."discount" AS "Sale_discount", "Sale"."vat" AS "Sale_vat", "Sale"."total" AS "Sale_total", "Sale"."close_date" AS "Sale_close_date", "Sale"."close_time" AS "Sale_close_time", "Sale"."account_id" AS "Sale_account_id" FROM "sales" "Sale" WHERE "Sale"."id" IN ($1) -- PARAMETERS: [603]
query: SELECT "sale_details"."id" AS "id", "sale_details"."sale_id" AS "sale_id" FROM "sale_details" "sale_details" WHERE (("sale_details"."sale_id" = $1)) -- PARAMETERS: [603]
query: START TRANSACTION
query: INSERT INTO "sale_details"("sale_id", "code", "name", "qty", "price", "discount", "vat_percent", "vat", "total") VALUES ($1, DEFAULT, $2, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT), ($3, DEFAULT, $4, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT, DEFAULT) RETURNING "id" -- PARAMETERS: [603,"Name1",603,"Name2"]
query: UPDATE "sale_details" SET "sale_id" = $1 -- PARAMETERS: [null]
query: COMMIT
No matter what I try it always updates sale_id to null to ALL rows! I can't figure out why? Please, Please, Please help!
Minimum reproduction code
https://stackblitz.com/edit/nestjs-starter-demo-2tg58a?file=src/app.service.ts
Steps to reproduce
- Needs a database and credentials set in app.module;
- Run upsert(body) in app.service
Expected behavior
After inserting the sale_details rows it should NOT update all the sale_ids to null instead it should update the just inserted rows to the sale_id of the sale not null.
query: UPDATE "sale_details" SET "sale_id" = $1 -- PARAMETERS: [null]
I can't explain where this query is coming from?!
It inserts the sale_deails correctly but after that, it messes the whole table up...
HELP!
Package version
^7.1.5
NestJS version
^7.5.1
Node.js version
v14.17.0
In which operating systems have you tested?
- macOS
- Windows
- Linux
Other
No response
I ran npm update, it updated
"@nestjs/common": "^7.6.18",
"@nestjs/core": "^7.6.18",
"@nestjs/platform-express": "^7.6.18",
and it is working as expected now (I think!)
Please report this to Typeorm's repo. This package is a small wrapper around it adding capabilities for Nest's DI context