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.06k stars 6.27k forks source link

Basic usage of adjacency-list throws exception #10001

Open spamshaker opened 1 year ago

spamshaker commented 1 year ago

Issue description

Tree repos not working, please see steps to reproduce

Expected Behavior

no exception

Actual Behavior

throws exception:

Cannot read properties of undefined (reading 'joinColumns') TypeError: Cannot read properties of undefined (reading 'joinColumns')

Steps to reproduce

https://orkhan.gitbook.io/typeorm/docs/tree-entities#adjacency-list

create entity from above example :

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

@Entity()
export class Category {
    @PrimaryGeneratedColumn()
    id: number

    @Column()
    name: string

    @Column()
    description: string

    @ManyToOne((type) => Category, (category) => category.children)
    parent: Category

    @OneToMany((type) => Category, (category) => category.parent)
    children: Category[]
}

usage:

  const connection = await new DataSource({
      type: 'sqlite',
      database: ':memory:',
      entities: [Category],
      synchronize: true,
      logging: true,
      logger: 'advanced-console'
    }).initialize();

    const categoryRepository = connection.getTreeRepository(Category);

    const category1 = new Category();
    category1.name = 'Category 1';
    await categoryRepository.save(category1);
    const categories = await categoryRepository.findTrees({ relations: ['children'] });
output:
Cannot read properties of undefined (reading 'joinColumns')
TypeError: Cannot read properties of undefined (reading 'joinColumns')

My Environment

Dependency Version
Operating System Mac OS 13 (ARM_64)
Node.js version 18.14.1
Typescript version 5.0.4
TypeORM version 0.3.15
sqlite3 5.1.6
ts-node 10.9.1

Additional Context

No response

Relevant Database Driver(s)

Are you willing to resolve this issue by submitting a Pull Request?

Not sure

leonardodimarchi commented 9 months ago

Hi @spamshaker, i've been struggling with that for a couple hours as well, but it appears that it is not possible to use TreeRepository with adjacency-list. More on that can be found here: https://github.com/typeorm/typeorm/issues/2540