Skip to content

Commit

Permalink
fix: findRoots should get the defined primary key column (#6982)
Browse files Browse the repository at this point in the history
* test: added failing test for github issue #6948

* fix: findRoots should get the actual primary key column, fixes #6948
  • Loading branch information
ilyasfoo committed Oct 30, 2020
1 parent 8244ea1 commit f2ba901
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/repository/TreeRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export class TreeRepository<Entity> extends Repository<Entity> {
const escapeAlias = (alias: string) => this.manager.connection.driver.escape(alias);
const escapeColumn = (column: string) => this.manager.connection.driver.escape(column);
const parentPropertyName = this.manager.connection.namingStrategy.joinColumnName(
this.metadata.treeParentRelation!.propertyName, "id"
this.metadata.treeParentRelation!.propertyName, this.metadata.primaryColumns[0].propertyName
);

return this.createQueryBuilder("treeEntity")
Expand Down
17 changes: 17 additions & 0 deletions test/github-issues/6948/entity/Category.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Entity, PrimaryGeneratedColumn, Column, Tree, TreeParent, TreeChildren } from "../../../../src";

@Entity()
@Tree("materialized-path")
export class Category {
@PrimaryGeneratedColumn()
cat_id: number;

@Column()
cat_name: string;

@TreeParent()
cat_parent: Category;

@TreeChildren({ cascade: true })
cat_children: Category[];
}
38 changes: 38 additions & 0 deletions test/github-issues/6948/issue-6948.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import "reflect-metadata";
import { Category } from "./entity/Category";
import { Connection } from "../../../src/connection/Connection";
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../../test/utils/test-utils";

describe("github issues > #6948 TreeRepository's findRoots query incorrectly when using a custom primary key", () => {
let connections: Connection[];
before(
async () =>
(connections = await createTestingConnections({
entities: [Category],
}))
);
beforeEach(() => reloadTestingDatabases(connections));
after(() => closeTestingConnections(connections));

it("entity parent column should work with custom primary column names ", () =>
Promise.all(
connections.map(async (connection) => {
const categoryRepository = connection.getTreeRepository(
Category
);
await categoryRepository.save(
categoryRepository.create({
cat_name: "Root node",
})
);
const rootNodes = await categoryRepository.findRoots();
rootNodes[0].should.deep.include({
cat_name: "Root node",
});
})
));
});

0 comments on commit f2ba901

Please sign in to comment.