Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

findDescendants method returns all direct subcategories (with its nested categories) #2764

Open
zengqiang365 opened this issue Sep 4, 2018 · 7 comments

Comments

@zengqiang365
Copy link

zengqiang365 commented Sep 4, 2018

my db is oracle, typeorm version is 0.2.7

i create an entity us ts like this:

@entity()
@Tree("materialized-path")
export class Organization {

@PrimaryColumn("int")
id: number;

@Column()
name: string;

@TreeChildren()
children: Organization[];

@TreeParent()
parent: Organization;

}

i convert ts to js,
then i save some entities:

var org1 = {
"id": 1,
"name": 'a'
};

var org2 = {
    "id": 2,
    "name": 'ab',
    "parent": { id: 1 }
};

var org3 = {
    "id": 3,
    "name": 'ac',
    "parent": { id: 1 }
};
var org4 = {
    "id": 4,
    "name": 'aba',
    "parent": { id: 2 }
};
var org5 = {
    "id": 5,
    "name": 'abb',
    "parent": { id: 2 }
};

var orepository = connection.getRepository("Organization");
orepository.save([org1, org2, org3, org4, org5])
    .then(function (savedorg) {
        console.log("Post has been saved: ", savedorg);
    }).catch(function (err) {
        console.log(err);
    });

then i use the findDescendantsTree:

connection.getTreeRepository("Organization").findDescendants({ id: 1 }).then(function (aaa) {
console.log(aaa)
}).catch(function (err) {
console.log(err)
})

it will returns all 5 entities , did i miss some options? or my way is wrong?

@GitStorageOne
Copy link

GitStorageOne commented Feb 5, 2019

Same issue with 0.2.13 & PG10

@GitStorageOne
Copy link

Here is complete test case. tree_bug.zip
Configured to work with PostgreSQL.
Empty database with name type_orm_test is required.

import { Entity, PrimaryGeneratedColumn, BaseEntity, Tree, TreeParent, TreeChildren, getTreeRepository, Column, createConnection } from 'typeorm';

@Entity()
@Tree('materialized-path')
export class NestingDoll extends BaseEntity {

  @PrimaryGeneratedColumn('uuid')
  id: string;

  @Column('varchar')
  name: string;

  @Column('uuid', { name: 'parentId', nullable: true })
  parentId: string;

  @TreeParent()
  parent: NestingDoll;

  @TreeChildren()
  children: NestingDoll[];

  async childrenItems(targetItem?: NestingDoll): Promise<NestingDoll[]> {
    if (targetItem) {
      const childItems = await getTreeRepository(NestingDoll).findDescendants(targetItem);
      return Promise.resolve(childItems);
    } else {
      const result = await getTreeRepository(NestingDoll).findRoots();
      return Promise.resolve(result);
    }
  }

}

async function initDefault() {
  const mainDoll: NestingDoll = NestingDoll.create({ name: 'mainDoll' });
  await mainDoll.save();

  const middleDoll: NestingDoll = NestingDoll.create({ name: 'middleDoll', parent: mainDoll });
  await middleDoll.save();

  const smallDoll: NestingDoll = NestingDoll.create({ name: 'smallDoll', parent: middleDoll });
  await smallDoll.save();

  const childItems = await mainDoll.childrenItems(mainDoll);
  for (const itm of childItems) {
    console.log(itm);
  }
};

(async () => {
  try {
    const connection = await createConnection();
    await connection.dropDatabase();
    await connection.synchronize();
    await initDefault();
  } catch (error) {
    console.log(error);
  }
})();

output:

NestingDoll {  id: '29ad062d-0699-4294-b03a-d1af639ec111',
  name: 'mainDoll',
  parentId: null }
NestingDoll {
  id: '8cf83132-64c0-439b-9842-c8e689ee0ec8',
  name: 'middleDoll',
  parentId: '29ad062d-0699-4294-b03a-d1af639ec111' }
NestingDoll {
  id: '931606f1-4aff-4aef-893e-f00808c4060d',
  name: 'smallDoll',
  parentId: '8cf83132-64c0-439b-9842-c8e689ee0ec8' }

@pleerock @vlapo Is it a bug?

@GitStorageOne
Copy link

Alternative way to get child items without tree repository:

async childrenItems(targetItem ?: NestingDoll): Promise < NestingDoll[] > {
  let result: NestingDoll[] = [];
  if(targetItem) {
    const targetItemEntity = await NestingDoll.findOne(targetItem.id, { relations: ['children'] });
    if (targetItemEntity) {
      result = targetItemEntity.children;
    }
  } else {
    result = await NestingDoll.find({ where: { parentId: null } });
  };
  return Promise.resolve(result);
}

@le0l1
Copy link

le0l1 commented Jun 29, 2019

or you can just find the descendants by parent
image

@buxel
Copy link

buxel commented Feb 7, 2020

is there any update on this?
it still seems to be broken in version 0.2.22 using @Tree("materialized-path") with sqlite3

@soroushahrari
Copy link

Is there any update on this?
findDescendants is returning all entities in postgres.

@astritsh
Copy link
Contributor

#8556 this can be related?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

7 participants