Skip to content

When the parent reference column of the entity is not the primary key, the mpath is incorrect when the parent of the tree entity is null. #9534

@Yuuki-Sakura

Description

@Yuuki-Sakura

Issue Description

When the parent of the entity is not the primary key, the mpath is incorrect when the parent of the updated tree entity is null.

example entities:

@Entity({ name: "categories" })
@Tree("materialized-path")
export class Category {
    @PrimaryGeneratedColumn()
    id: number

    @Column({
        type: "varchar",
        name: "uid",
        unique: true,
    })
    uid: string

    @Column()
    name: string

    @Column({
        type: "varchar",
        name: "parentUid",
        nullable: true,
    })
    parentUid?: string | null

    @TreeParent()
    @JoinColumn({
        name: "parentUid",
        referencedColumnName: "uid"
    })
    parentCategory?: Category | null

    @TreeChildren({ cascade: true })
    childCategories: Category[]
}

Expected Behavior

the update sql logging should be

UPDATE "categories" SET "mpath" = REPLACE(mpath, 'a1.a11.a111.', 'a111.') WHERE "mpath" LIKE $1 -- PARAMETERS: ["a1.a11.a111.%"]
UPDATE "categories" SET "parentUid" = $1 WHERE "id" IN ($2) -- PARAMETERS: [null,3]

Actual Behavior

UPDATE "categories" SET "mpath" = REPLACE(mpath, 'a111.', 'a111.') WHERE "mpath" LIKE $1 -- PARAMETERS: ["a111.%"]
UPDATE "categories" SET "parentUid" = $1 WHERE "id" IN ($2) -- PARAMETERS: [null,3]

Steps to Reproduce

        const categoryRepository =
            connection.getTreeRepository(Category)

        const a1 = new Category()
        a1.name = "a1"
        a1.uid="a1"
        await categoryRepository.save(a1)

        const a11 = new Category()
        a11.name = "a11"
        a11.uid="a11"
        a11.parentCategory = a1
        await categoryRepository.save(a11)

        const a111 = new Category()
        a111.name = "a111"
        a111.uid="a111"
        a111.parentCategory = a11
        await categoryRepository.save(a111)

        a111.parentCategory = null

        await categoryRepository.save(a111)

My Environment

Dependency Version
Operating System macOS Ventura arm64
Node.js version v16.16.0
Typescript version 4.8.2
TypeORM version 0.3.10 and 0.2.45

Relevant Database Driver(s)

DB Type Reproducible
aurora-mysql no
aurora-postgres 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
spanner 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.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions