typeorm-polymorphic icon indicating copy to clipboard operation
typeorm-polymorphic copied to clipboard

Relation with property path ** in entity was not found

Open woobottle opened this issue 4 years ago • 3 comments

Paragraphs.entity.ts

import {
  Column,
  PrimaryGeneratedColumn,
  ManyToOne,
  Entity,
  JoinColumn,
  OneToMany,
  JoinTable,
} from 'typeorm';
import { DateAudit } from '@entities/date-audit.entity';
import { Posts as Post } from '@posts/posts.entity';
import { Likes as Like } from '@likes/likes.entity';
import { Comments as Comment } from '@comments/comments.entity';
import { PolymorphicChildren } from 'typeorm-polymorphic';

@Entity()
export class Paragraphs extends DateAudit {
  @PrimaryGeneratedColumn()
  id: number;

  @Column()
  body: string;

  @Column({
    default: 0,
  })
  viewCount: number;

  @ManyToOne(() => Post, (post) => post.paragraphs)
  @JoinColumn({ name: 'post_id' })
  post: Post;

  @OneToMany(() => Comment, (comment) => comment.paragraph)
  comments: Comment[];

  @PolymorphicChildren(() => Like, { eager: true })
  likes: Like[];
}

likes.entity.ts

import { Column, ManyToOne, Entity, PrimaryGeneratedColumn } from 'typeorm';
import { DateAudit } from '@entities/date-audit.entity';
import { Paragraphs as Paragraph } from '@paragraphs/paragraphs.entity';
import { PolymorphicChildInterface } from 'typeorm-polymorphic/dist/polymorphic.interface';
import { PolymorphicParent } from 'typeorm-polymorphic';

@Entity()
export class Likes extends DateAudit implements PolymorphicChildInterface {
  @PrimaryGeneratedColumn()
  id: number;

  @PolymorphicParent(() => [Paragraph], { eager: false })
  owner: Paragraph;

  @Column({ name: 'entity_id', nullable: true, type: 'int' })
  entityId: number;

  @Column({ name: 'entity_type', nullable: true })
  entityType: string;
}

posts.entity.ts

import {
  Column,
  Entity,
  JoinColumn,
  ManyToOne,
  OneToMany,
  PrimaryGeneratedColumn,
} from 'typeorm';
import { DateAudit } from '@entities/date-audit.entity';
import { Presses as Press } from '@presses/presses.entity';
import { Paragraphs as Paragraph } from '@paragraphs/paragraphs.entity';
import { Categories as Category } from '@categories/categories.entity';

@Entity()
export class Posts extends DateAudit {
  @PrimaryGeneratedColumn()
  id: number;

  @Column({ nullable: true })
  title: string;

  @ManyToOne(() => Press, (press) => press.posts)
  @JoinColumn({ name: 'press_id' })
  press: Press;

  @Column({ nullable: true })
  image: string;

  @OneToMany(() => Paragraph, (paragraph) => paragraph.post)
  paragraphs: Paragraph[];

  @ManyToOne(() => Category, (category) => category.posts)
  @JoinColumn({ name: 'category_id' })
  category: Category;
}

posts.repository.ts

import {
  getConnection,
  EntityRepository,
  In,
  Repository,
  getRepository,
} from 'typeorm';
import { Likes as Like } from '@likes/likes.entity';
import { Comments as Comment } from '@comments/comments.entity';
import { Paragraphs as Paragraph } from '@paragraphs/paragraphs.entity';
import { AbstractPolymorphicRepository } from 'typeorm-polymorphic';
import { Posts as Post } from './posts.entity';

@EntityRepository(Post)
export class PostsRepository extends AbstractPolymorphicRepository<Post> {
  async findById(id: number): Promise<Post> {
    const post = await getRepository(Post)
      .createQueryBuilder('post')
      .leftJoinAndSelect('post.paragraphs', 'paragraphs')
      .innerJoinAndSelect('paragraphs.comments', 'comments')
      .leftJoinAndSelect(
        'paragraphs.likes',
        'likes',
      )
      .where('post.id = :id', { id })
      .getOne();
    return post;
  }
}

i want use 'paragraphs.likes' in PostsRepository but it always return [ExceptionsHandler] Relation with property path likes in entity was not found. plz help me i want use this

woobottle avatar Jun 22 '21 05:06 woobottle

i want use 'paragraphs.likes' in PostsRepository but it always return [ExceptionsHandler] Relation with property path likes in entity was not found. plz help me i want use this

assumption: you need to define the relationship on the Posts end, or define an inverse relationship. I've been trying to figure out related things lately. I'm not 100% sure if this library actually works as intended, I'm looking into seeing if I can PR something or actually misunderstand.

nmokkenstorm avatar Jun 22 '21 14:06 nmokkenstorm

assumption: you need to define the relationship on the Posts end, or define an inverse relationship. I've been trying to figure out related things lately. I'm not 100% sure if this library actually works as intended, I'm looking into seeing if I can PR something or actually misunderstand.

if you find something useful thing please notice that thanks for your reply

also i will find to solve this

woobottle avatar Jun 22 '21 15:06 woobottle

Did u manage to find any solution for this case?

shathaabualrob avatar Oct 05 '22 11:10 shathaabualrob