I'm working on a express (with TypeORM) + ReactJS app.
The problem is that I have 3 entities linked by OneToMany relationship like this:
- customer
- product(linked to customer)
- model(linked to product)
import { Product } from './product.entity'
@Entity('customer')
export class Customer extends BaseEntity{
@PrimaryGeneratedColumn()
readonly id: number;
@Column ({name: 'name'})
name : string;
@Column ({name: 'test', nullable: true})
test : string;
@OneToMany(() => Product, product => product.customer)
// @JoinColumn({ name: 'product_id' })
products: Product[]
}
import {Customer} from './customer.entity'
import {Model} from './model.entity'
@Entity('product')
export class Product extends BaseEntity{
@PrimaryGeneratedColumn()
readonly id: number;
@Column ({name: 'name'})
name : string;
@Column ({name: 'test', nullable: true})
test : string;
@Column ({name: 'deleted', nullable: true})
deleted : string;
@ManyToOne(() => Customer, customer => customer.products)
@JoinColumn({ name: 'customer_id' })
customer: Customer;
@OneToMany(() => Model, model => model.product)
@JoinColumn({ name: 'customer_id' })
models: Model[]
}
import { Product } from "./product.entity";
@Entity('model')
export class Model extends BaseEntity{
@PrimaryGeneratedColumn()
readonly id: number;
@Column ({name: 'name'})
name : string;
@Column ({name: 'size', nullable: true})
size : string;
@Column ({name: 'deleted', nullable: true})
deleted : string;
@ManyToOne(() => Product, product => product.models)
@JoinColumn({ name: 'product_id' })
product: Product;
}
The save method from Express is:
static add = async(req: Request, res)=> {
const connection = getConnection();
const queryRunner = connection.createQueryRunner();
await queryRunner.connect();
await queryRunner.startTransaction();
try {
let customer: Customer
customer = await queryRunner.manager.getRepository(Customer).findOneOrFail(req.body.id)
const productsReq: Array<Product> = req.body.products
productsReq.forEach(async (product: any) => {
let updatedProd = {...product, customer: customer}
const savedProd = await queryRunner.manager.getRepository(Product).save({...updatedProd})
product.models.forEach(async (model: any) => {
let updatedModel = {...model, product: savedProd}
await queryRunner.manager.getRepository(Model).save(updatedModel)
});
});
await queryRunner.commitTransaction();
return res.send('Saving done')
} catch (error) {
console.log(error)
await queryRunner.rollbackTransaction();
res.status(500).send('Some error occurs');
} finally {
}
}
Currently, in the DB, I have the following data:
1 customer with id 30
name | id | test |
---|---|---|
first customer | 30 | test column |
1 product with id 119 linked to customer 30
id | name | test | customer_id | deleted |
---|---|---|---|---|
119 | first product | test column | 30 |
2 models with ids: 90 and 91 linked to product 119
id | name | size | deleted | product_id |
---|---|---|---|---|
91 | second model witout id | 2000 | 119 | |
90 | first model with id | 1000 | 119 |
Next, from React i'm trying to update only the model with id 90 and add a new model. (So i'm not sending to backend all the models, model with id 91 is not sent).
The JSON object sent from frontend to backend looks like this:
{
"id": 30,
"name": "first customer",
"test": "test column",
"products": [
{
"id" : 119,
"name": "first product",
"test": "test column",
"models": [
{
"id": 90,
"name": "first model with id",
"size": 1000
},
{
"name": "second model witout id",
"size": 2000
}
]
}
]
}
But the problem is in DB the foreing key "product_id" on table "model" is set to null for model with id 91 and a new row(92) is inserted.
The result is:
|id|name|size|deleted|product_id|
|--|----|----|-------|----------|
|91|second model witout id|2000|||
|90|first model with id|1000||119|
|92|second model witout id|2000||119|
How can I add a new model and update an existing one without sending all the existing models in DB ?