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

fix: findRoots should get the defined primary key column #6982

Merged
merged 2 commits into from
Oct 30, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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",
});
})
));
});