typeorm / typeorm

ORM for TypeScript and JavaScript. Supports MySQL, PostgreSQL, MariaDB, SQLite, MS SQL Server, Oracle, SAP Hana, WebSQL databases. Works in NodeJS, Browser, Ionic, Cordova and Electron platforms.
http://typeorm.io
MIT License
34.28k stars 6.32k forks source link

Cannot query across many-to-many for property subject typeorm with nestjs? #9863

Open siamahnaf opened 1 year ago

siamahnaf commented 1 year ago

What was unclear or otherwise insufficient?

I am getting one error with typerom and postgreesql- Cannot query across many-to-many for property subject when I try to update my entity. I am not understanding where the problem is. Please help me someone!

Here is my entity-

import { Entity, Column, PrimaryGeneratedColumn, ManyToOne, ManyToMany, UpdateDateColumn, CreateDateColumn, JoinTable } from "typeorm";
//Orm Entity
import { User } from "src/user/model/user.entity";
import { Class } from "src/classes/model/class.entity";
import { Subject } from "src/subject/model/subject.entity";
@Entity()
export class Student {
    @PrimaryGeneratedColumn("uuid")
    id: string;
    @ManyToOne(() => Class)
    class: Class;
    @ManyToMany(() => Subject, { cascade: true })
    @JoinTable()
    subject: Subject[];
    @Column({ type: "text" })
    studentId: string;
    @Column({ type: "text" })
    name: string;
    @Column({ type: "text", nullable: true })
    image: string;
    @Column({ type: "text" })
    gender: string;
    @Column({ type: "text" })
    dob: string;
    @Column({ type: "text", nullable: true })
    blood: string;
    @Column({ type: "text", nullable: true })
    religion: string;
    @Column({ type: "text", nullable: true })
    number: string;
    @Column({ type: "text", nullable: true })
    email: string;
    @Column({ type: "text" })
    date: string;
    @Column({ type: "text", nullable: true })
    birthCertificate: string;
    @Column({ type: "text" })
    fatherName: string;
    @Column({ type: "text", nullable: true })
    fatherNidNumber: string;
    @Column({ type: "text", nullable: true })
    fatherPhone: string;
    @Column({ type: "text" })
    motherName: string;
    @Column({ type: "text", nullable: true })
    motherNidNumber: string;
    @Column({ type: "text", nullable: true })
    motherPhone: string;
    @Column({ type: "text", nullable: true })
    guardianName: string;
    @Column({ type: "text", nullable: true })
    guardianNidNumber: string;
    @Column({ type: "text", nullable: true })
    guardianPhone: string;
    @Column({ type: "text" })
    address: string;
    @Column({ type: "text" })
    school: string;
    @ManyToOne(() => User)
    createdBy: User;
    @UpdateDateColumn({ type: "timestamptz" })
    updated_at: Date;
    @CreateDateColumn({ type: "timestamptz" })
    created_at: Date;
}

And I am getting error when I try to update studentEntity-

async update(studentInput: StudentInput, id: string, reqUser: ReqUser) {
        const subjects: Subject[] = await Promise.all(
            studentInput.subject.map((id: string) => this.subjectRepository.findOneBy({ id: id }))
        );
        const result = await this.studentRepository.update(id, {
            ...studentInput,
            class: { id: studentInput.class },
            createdBy: { id: reqUser.id },
            subject: subjects
        });
        if (result.affected === 0) throw new NotFoundException("Student not found!");
        return {
            success: true,
            message: "Student updated successfully!"
        }
    };

I create new data successfully by this way. But when I try to update my student, I am getting the error. Here I am building my project with nestjs

Recommended Fix

Complete Error-

[Nest] 13464  - 03/17/2023, 6:41:36 PM   ERROR [ExceptionsHandler] Cannot query across many-to-many for property subject
Error: Cannot query across many-to-many for property subject
    at UpdateQueryBuilder.createPropertyPath (G:\Web Devlopment\E-Campus\server\src\query-builder\QueryBuilder.ts:1367:27)
    at UpdateQueryBuilder.createUpdateExpression (G:\Web Devlopment\E-Campus\server\src\query-builder\UpdateQueryBuilder.ts:493:18)
    at UpdateQueryBuilder.getQuery (G:\Web Devlopment\E-Campus\server\src\query-builder\UpdateQueryBuilder.ts:53:21)
    at UpdateQueryBuilder.getQueryAndParameters (G:\Web Devlopment\E-Campus\server\src\query-builder\QueryBuilder.ts:507:28)
    at UpdateQueryBuilder.execute (G:\Web Devlopment\E-Campus\server\src\query-builder\UpdateQueryBuilder.ts:142:50)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at StudentService.update (G:\Web Devlopment\E-Campus\server\src\student\student.service.ts:107:24)
    at target (G:\Web Devlopment\E-Campus\server\node_modules\@nestjs\core\helpers\external-context-creator.js:74:28)
    at Object.updateStudent (G:\Web Devlopment\E-Campus\server\node_modules\@nestjs\core\helpers\external-proxy.js:9:24)

I already place a question on stackoverflow and slack chennel. But I am not getting any response. That's why I create this isee?

Aravin commented 1 year ago

I have a similar issue, did anyone find a solution?

@siamahnaf

pedro-areal-torres commented 1 year ago

Hey! Did you manage to find a solution? @Aravin / @siamahnaf

BaharaJr commented 9 months ago

Hey! Did you manage to find a solution? @Aravin / @siamahnaf

I had a similar issue; the solution I used was to use save instead of update and since I already have the ID, it updates the existing record.

alexandrenolla commented 6 months ago

I have the same issue here, and it doesn't work with save also, because all the data from Dto is updated, except the related ids. In any case, .save doesn't bring any errors and the entity is partial updated, while .update brings this error log.

kfern commented 6 months ago

Same here. It works with save as @BaharaJr . The same data failed with update