TypeORM Cannot query across one-to-many for property error

See original GitHub issue

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 ?

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:4
  • Comments:10

github_iconTop GitHub Comments

10reactions
karladlercommented, Nov 24, 2021

same problem here using EntityName.update(id, data) directly. Still works for me using active record style:

const instance = await EntityName.findOne(id);
Object.assign(instance, data);
instance.save();

typeorm@0.2.37

2reactions
Victornfbcommented, Nov 18, 2022

For those who are using repository, this worked for me:

Obs.: I’m using "typeorm": "^0.3.10"

this.repository = connectionSource.getRepository(EntityName);

const entityName = await this.repository.findOneBy({id});

Object.assign(entityName, data);

await this.repository.save(entityName);
Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot query across one-to-many for property NestJS and ...
I feel like I am missing something but I am not sure. Have referred both NestJS and TypeORM docs but can't seem to...
Read more >
error: cannot query across one-to-many for property members ...
I am new with typeorm and I face trouble with a OneToMany relationship. I have a Person entity which contains a @OnetoMany relationship...
Read more >
Many-to-one / one-to-many relations - typeorm - GitBook
Many-to-one / one-to-many is a relation where A contains multiple instances of B, but B contains only one instance of A. Let's take...
Read more >
Joins and Queries with Different ORM Tools
You can omit @JoinColumn in a @ManyToOne / @OneToMany relation. @OneToMany cannot exist without @ManyToOne. If you want to use @OneToMany, @ ...
Read more >
Typeorm query on ManyToMany relations-postgresql
I'm trying to query over the table products to find, Certain products that have "at least" Category "Food", "Cold". I hope someone can...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found