Reputation: 73
I have implemented one to one and one to many relations between tables.
Main table Business
@Entity()
export class Business {
@PrimaryGeneratedColumn()
readonly id: number;
@Column({ nullable: true })
name: string;
@OneToOne(type => Review, {
nullable: true,
cascade: true,
})
@JoinColumn({ name: 'review_id' })
review: Review;
Review Table
@Entity()
export class Review {
@PrimaryGeneratedColumn()
id: number;
@CreateDateColumn({ type: 'timestamptz' })
created_at: Date;
@OneToMany(type => Task, task => task.review, {
cascade: true,
nullable: true,
})
tasks: Task[];
Task Table
@Entity()
export class Task {
@PrimaryGeneratedColumn()
id: number;
@Column({ nullable: true })
message: string;
@Column({ nullable: true })
category: string;
@ManyToOne(type => Review, review => review.tasks)
review: Review;
const businessRepo = connection.getRepository(Business);
const business = await businessRepo.findOne({
where: { id: id },
});
let review: Review = new Review();
review.created_at = created_at;
business.review = review
const tasks: Task[] = [];
let task: Task = new Task();
task.message = 'some message';
task.category = 'some category';
tasks.push(task);
await businessRepo.update(
business.id,
business,
);
I am facing error while updating the business object.
(node:14432) UnhandledPromiseRejectionWarning: Error: Cannot query across one-to-many for property tasks
I tried the workaround by trying to make cascade as false, but in vain. I do not want to make cascade update with single statement, as I have many customisations while updating task and so I am handling the task update separately.
Do you know how I can fix this issue?
Upvotes: 2
Views: 8705
Reputation: 31
We need to use queryBuilder for cases like this since find doesn't allow filtering relations. we can use find methods but it will end code structure with lot of complications and unreadability:
Upvotes: 3