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

QueryFailedError when use tree entity #8443

Closed
Yuuki-Sakura opened this issue Dec 10, 2021 · 0 comments · Fixed by #8447
Closed

QueryFailedError when use tree entity #8443

Yuuki-Sakura opened this issue Dec 10, 2021 · 0 comments · Fixed by #8447

Comments

@Yuuki-Sakura
Copy link
Contributor

Issue Description

QueryFailedError when use TreeRepository

Expected Behavior

SELECT "treeEntity"."id" AS "treeEntity_id", "treeEntity"."code" AS "treeEntity_code", "treeEntity"."name" AS "treeEntity_name", "treeEntity"."longitude" AS "treeEntity_longitude", "treeEntity"."latitude" AS "treeEntity_latitude", "treeEntity"."level" AS "treeEntity_level", "treeEntity"."mpath" AS "treeEntity_mpath", "treeEntity"."parent_id" AS "treeEntity_parent_id" FROM "district" "treeEntity" WHERE "treeEntity"."parent_id" IS NULL

Actual Behavior

[Nest Shop] [INFO] [2021-12-10 14:18:52.819 PM +0800] [TypeORM] [query-id: pytz79nUvUl3y6ZVrCwm] query: SELECT "treeEntity"."id" AS "treeEntity_id", "treeEntity"."code" AS "treeEntity_code", "treeEntity"."name" AS "treeEntity_name", "treeEntity"."longitude" AS "treeEntity_longitude", "treeEntity"."latitude" AS "treeEntity_latitude", "treeEntity"."level" AS "treeEntity_level", "treeEntity"."mpath" AS "treeEntity_mpath", "treeEntity"."parent_id" AS "treeEntity_parent_id" FROM "district" "treeEntity" WHERE "treeEntity"."parentId" IS NULL - {}
[Nest Shop] [ERROR] [2021-12-10 14:18:52.843 PM +0800] [TypeORM] [query-id: pytz79nUvUl3y6ZVrCwm] query: SELECT "treeEntity"."id" AS "treeEntity_id", "treeEntity"."code" AS "treeEntity_code", "treeEntity"."name" AS "treeEntity_name", "treeEntity"."longitude" AS "treeEntity_longitude", "treeEntity"."latitude" AS "treeEntity_latitude", "treeEntity"."level" AS "treeEntity_level", "treeEntity"."mpath" AS "treeEntity_mpath", "treeEntity"."parent_id" AS "treeEntity_parent_id" FROM "district" "treeEntity" WHERE "treeEntity"."parentId" IS NULL - {
  stack: [
    {
      length: 185,
      name: 'error',
      severity: 'ERROR',
      code: '42703',
      hint: 'Perhaps you meant to reference the column "treeEntity.parent_id".',
      position: '406',
      file: 'parse_relation.c',
      line: '3503',
      routine: 'errorMissingColumn'
    }
  ]
}

Steps to Reproduce

  1. add tree entity and JoinColumn decorator
import {
  Column,
  Entity,
  Index,
  JoinColumn,
  PrimaryGeneratedColumn,
  Tree,
  TreeChildren,
  TreeParent,
} from 'typeorm';

export enum DistrictLevel {
  Country = 'country', //国家
  Province = 'province', //省份
  City = 'city', //城市
  District = 'district', //县/区
  Street = 'street', //街道
}

@Entity('district')
@Tree('materialized-path')
export class DistrictEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Column('varchar', {
    length: 6,
  })
  @Index()
  code: string;

  @Column()
  @Index()
  name: string;

  get location() {
    return this.longitude + ',' + this.latitude;
  }

  @Column({
    comment: '经度',
  })
  longitude: string;

  @Column({
    comment: '纬度',
  })
  latitude: string;

  @Column('simple-enum', {
    comment: '地区级别',
    enum: DistrictLevel,
  })
  level: DistrictLevel;

  @TreeParent()
  @JoinColumn({
    name: 'parent_id',
  })
  parent: DistrictEntity;

  @TreeChildren({
    cascade: true,
  })
  children: DistrictEntity[];
}
getConnection().getTreeRepository(DistrictEntity).findTrees();

My Environment

Dependency Version
Operating System Ubuntu 20.04
Node.js version v14.17.1
Typescript version ^4.4.4
TypeORM version ^0.2.41
NestJS version ^8.2.2

Additional Context

can use this repository https://github.com/Yuuki-Sakura/nest-shop/tree/typeorm-issue

Relevant Database Driver(s)

DB Type Reproducible
aurora-data-api no
aurora-data-api-pg no
better-sqlite3 no
cockroachdb no
cordova no
expo no
mongodb no
mysql no
nativescript no
oracle no
postgres yes
react-native no
sap no
sqlite no
sqlite-abstract no
sqljs no
sqlserver no

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

  • ✖️ Yes, I have the time, and I know how to start.
  • ✅ Yes, I have the time, but I don't know how to start. I would need guidance.
  • ✖️ No, I don’t have the time, but I can support (using donations) development.
  • ✖️ No, I don’t have the time and I’m okay to wait for the community / maintainers to resolve this issue.
Yuuki-Sakura added a commit to Yuuki-Sakura/typeorm that referenced this issue Dec 13, 2021
AlexMesser pushed a commit that referenced this issue Feb 15, 2022
* fix: QueryFailedError when tree entity with joinColumns (#8443)

* test: add test for #8443
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant