error: cannot query across one-to-many for property members
See original GitHub issueI 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:
- Created 2 years ago
- Comments:5 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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
many thanks for the hint. I will give a try with relationId.