question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

error: cannot query across one-to-many for property members

See original GitHub issue

I am new with typeorm and I face trouble with a OneToMany relationship. I have a Person entity which contains a @OnetoMany relationship to a Member entity and a @ManyToOne relationship to a Fare entity. I can post a new Person without problem but I get an error when I update an existing Person with the following body:

{"firstName":"john","lastName":"doe","fareId": 1} The error is the following:

(node:699940) UnhandledPromiseRejectionWarning: Error: Cannot query across one-to-many for property members Here is my Person entity:

import { Entity, Column, ManyToOne, OneToMany, PrimaryGeneratedColumn } from "typeorm";

import { Fare, Member } from "./";

@Entity()
export class Person {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({
        unique: false,
        nullable: false
    })
    firstName: string;

    @Column({
        unique: false,
        nullable: false
    })
    lastName: string;

    @Column({
        unique: false,
        nullable: true
    })
    address: string;

    @Column({
        unique: false,
        nullable: true
    })
    phone: string;

    @Column({
        unique: false,
        nullable: true
    })
    email: string;

    @OneToMany(() => Member, member => member.person)
    members: Member[];

    @ManyToOne(() => Fare, fare => fare.persons)
    fare: Fare
}

Here is the update part of my controller:

router.put('/persons/:id', async (req: Request, res: Response) => {
    const id = +req['params']['id'];
    const person = await this._personService.readOne(id);
    if (!person) {
        res.status(400).send('Person not found')
    }

    const { lastName, firstName, address, email, phone, fareId } = req['body'];
    if (fareId) {
        const fare = await this._fareService.readOne(fareId);
        if (!fare) {
            res.status(400).send('Fare not found')
        } else {
            person.fare = fare;
        }
    }

    person.lastName = lastName ? lastName : person.lastName;
    person.firstName = firstName ? firstName : person.firstName;
    person.address = address ? address : person.address;
    person.email = email ? email : person.email;
    person.phone = phone ? phone : person.phone;

    const updatedPerson = await this._personService.update(person,id);
    res.send(updatedPerson);
});

and here is the update part of my Person service:

public update = async (person: Person, id: number) => {
    const updatedPerson = await this._personRepository.update(id, person);
    return updatedPerson;
}

would you know what is wrong with my code ?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

28reactions
n4ancommented, Dec 11, 2021

Thanks for the quick answer. So, there is no way to pass around that problem ? Should I consider to change of ORM ?

to which ORM you switched? Sequelize is good but no update with relations out of box Prisma has strange model definition Typeorm not have select some fields from relations and filter by relations and always push you querybuilder

No normal ORM for NodeJS

Remains to see what is MicroORM

0reactions
eurydice76commented, Nov 16, 2021

many thanks for the hint. I will give a try with relationId.

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 >
Many-to-many relations - typeorm - GitBook
Let's take for example Question and Category entities. A question can have multiple categories, and each category can have multiple questions. @JoinTable() is ......
Read more >
Many-to-many relations | TypeORM Docs
Many-to-many is a relation where A contains multiple instances of B, and B contain multiple instances of A. Let's take for example Question...
Read more >
TypeORM - Quick Guide - Tutorialspoint
TypeORM supports multiple databases like MySQL, PostgreSQL, MariaDB, SQLite, ... However, @OneToMany cannot exist without @ManyToOne and @ManyToOne property ...
Read more >
Adding the ManyToOne Relation - SymfonyCasts
So far, in order to add a new column to a table, we add a new property to the corresponding entity. And, at...
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