typeorm: TypeORM Cannot query across one-to-many for property error
Hi all,
I have the following two entities:
import { Entity, Column, PrimaryGeneratedColumn, ManyToOne } from "typeorm";
import { Activity } from "./";
@Entity()
export class Fare {
@PrimaryGeneratedColumn()
id: number;
@Column({unique: false,nullable: false})
name: string;
@Column({type: "float",unique: false,nullable: false})
price: number;
@ManyToOne(() => Activity, activity => activity.fares)
activity: Activity;
}
and
import { Entity, Column, OneToMany, PrimaryGeneratedColumn } from "typeorm";
import { Fare } from "./";
@Entity()
export class Activity {
@PrimaryGeneratedColumn()
id: number;
@Column({unique: true,nullable: false})
name: string;
@Column({unique: false,nullable: true})
description: string;
@OneToMany(() => Fare, (fare) => fare.activity, {cascade: true})
fares: Fare[];
}
and I want to update an activity using the following body:
{"id":10, "name":"dddd", "fares":[{"id":5,"name":"jeune","price":20.0}]}
To do so, I implemented the following service:
import { singleton } from 'tsyringe'
import { getConnection, Timestamp } from 'typeorm';
import { Activity } from '../entities/';
import { ActivityRepository } from '../repositories'
import { Fare } from '../entities'
@singleton()
export class ActivityService {
constructor(private _activityRepository: ActivityRepository) {
this._activityRepository = getConnection("lasso").getCustomRepository(ActivityRepository);
}
public update = async (activity: Activity, id: number) => {
const updatedActivity = await this._activityRepository
.createQueryBuilder()
.update(Activity)
.set(activity)
.where("id = :id", { id: id })
.execute();
return updatedActivity;
}
};
When running my update request I get the following error:
(node:204854) UnhandledPromiseRejectionWarning: Error: Cannot query across one-to-many for property fares
From a previous related issue, the answer was to use query builder for such tasks. Does it mean that the way I did it is wrong ? Could you please show me what is wrong with my query ?
About this issue
- Original URL
- State: open
- Created 3 years ago
- Reactions: 10
- Comments: 17
same problem here using
EntityName.update(id, data)directly. Still works for me using active record style:typeorm@0.2.37
For those who are using repository, this worked for me:
Obs.: I’m using
"typeorm": "^0.3.10"any updates ?
The solution from @karladler should work for you as well, can you try that. Create an empty object, assign it and then save.