Skip to content
Advertisement

EntityColumnNotFound: No entity column “authors” was found. many to many relations on TypeORM and NestJS

I am trying to get the books per user from the get request I have the following problem, it turns out that I am doing a many-to-many relationship but it indicates that the authors entity was not found, I have already searched the TypeORM documentation but I cannot find anything, this is my code:

book.controller.ts

@Controller('book')
export class BookController {
    constructor(
        private readonly _bookService: BookService
    ) { }

    @Get('author/:authorId')
    getBooksByAuthor(
      @Param('authorId', ParseIntPipe) authorId: number,
    ): Promise<ReadBookDto[]> {
      return this._bookService.getBooksByAuthor(authorId);
    }

}

book.service.ts

    @Injectable()
    export class BookService {
        constructor(
            @InjectRepository(BookRepository)
            private readonly _bookRepository: BookRepository,
    
            @InjectRepository(UserRepository)
            private readonly _userRepository: UserRepository
        ) { }
    
        async getBooksByAuthor(authorId: number): Promise<ReadBookDto[]> {
    
            const books: Book[] = await this._bookRepository.find({

// This is where I have the entity of authors, it should be noted that the entity of books if I have it

                where: { status: status.ACTIVE, authors: In([authorId]) },
            })
            console.log(books)
            return books.map(book => plainToClass(ReadBookDto, book));
        }
    }

book.entity.ts

import {
  BaseEntity,
  PrimaryGeneratedColumn,
  Column,
  ManyToMany,
  Entity,
  JoinColumn,
  CreateDateColumn,
  UpdateDateColumn,
} from 'typeorm';
import { User } from '../user/user.entity';

@Entity('books')
export class Book extends BaseEntity {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ type: 'varchar', length: 100, nullable: false })
  name: string;

  @Column({ type: 'varchar', length: 500 })
  description: string;

  @ManyToMany(type => User, user => user.books, { eager: true, primary: true})
  @JoinColumn()
  authors: User[];

  @Column({ type: 'varchar', default: 'ACTIVE', length: 8 })
  status: string;

  @CreateDateColumn({ type: 'timestamp', name: 'created_at' })
  createdAt: Date;

  @UpdateDateColumn({ type: 'timestamp', name: 'updated_at' })
  updatedAt: Date;
}

This is the user entity where I make the many to many relation user.entity.ts

@Entity('users')
export class User extends BaseEntity {
  @PrimaryGeneratedColumn('increment')
  id: number;

  @Column({ type: 'varchar', unique: true, length: 25, nullable: false })
  username: string;

  @Column({ type: 'varchar', nullable: false })
  email: string;

  @Column({ type: 'varchar', nullable: false })
  password: string;

  @OneToOne(type => UserDetails, {
    cascade: true,
    nullable: false,
    eager: true,
  })
  @JoinColumn({ name: 'detail_id' })
  details: UserDetails;


  @ManyToMany(type => Book, book => book.authors)
  @JoinTable({ name: 'user_books' })
  books: Book[];

}

If you could help me find the error it would be very helpful Thanks

Advertisement

Answer

You can use queryBuilder to get the books:

 @Injectable()
export class BookService {
    constructor(
        @InjectRepository(BookRepository)
        private readonly _bookRepository: BookRepository,

        @InjectRepository(UserRepository)
        private readonly _userRepository: UserRepository
    ) { }

    async getBooksByAuthor(authorId: number): Promise<ReadBookDto[]> {

       const books: Book[] = await this._bookRepository.createQueryBuilder('books')
                 .leftJoinAndSelect("books.authors", "users")
                 .where('books.status = :status',{status : status.ACTIVE})
                 .andWhere("users.id = :id ", { id: authorId })
                  .getMany();

        console.log(books)
        return books.map(book => plainToClass(ReadBookDto, book));
    }
}
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement