medusajs / medusa

The world's most flexible commerce platform.
https://medusajs.com
MIT License
25.45k stars 2.53k forks source link

Get api works on post or update api getting Cyclic dependency: "ProductCategory" TypeORMError: Cyclic dependency: "ProductCategory" #7222

Closed veract-dev closed 5 months ago

veract-dev commented 5 months ago

I'm trying to add 1 extra column is_primary in product-category entity, but got Cyclic dependency error

TypeORMError: Cyclic dependency: "ProductCategory" at visit (C:\Users\Public\Development\beanstalk\dev\bean-stalk-backend\node_modules\typeorm\persistence\SubjectTopoligicalSorter.js:147:23) at visit (C:\Users\Public\Development\beanstalk\dev\bean-stalk-backend\node_modules\typeorm\persistence\SubjectTopoligicalSorter.js:164:21) at SubjectTopoligicalSorter.toposort (C:\Users\Public\Development\beanstalk\dev\bean-stalk-backend\node_modules\typeorm\persistence\SubjectTopoligicalSorter.js:143:17) at SubjectTopoligicalSorter.sort (C:\Users\Public\Development\beanstalk\dev\bean-stalk-backend\node_modules\typeorm\persistence\SubjectTopoligicalSorter.js:53:45) at SubjectExecutor.execute (C:\Users\Public\Development\beanstalk\dev\bean-stalk-backend\node_modules\typeorm\persistence\SubjectExecutor.js:91:108) at EntityPersistExecutor.execute (C:\Users\Public\Development\beanstalk\dev\bean-stalk-backend\node_modules\typeorm\persistence\EntityPersistExecutor.js:140:36) at process.processTicksAndRejections (node:internal/process/task_queues:95:5)

Medusa version : 1.20.6

CODE:

models/product-category.ts

import { Column, Entity, Tree } from "typeorm"
import {
    // alias the core entity to not cause a naming conflict
    ProductCategory as MedusaProductCategory,
} from "@medusajs/medusa"

@Entity()
@Tree("materialized-path")
export class ProductCategory extends MedusaProductCategory {
    @Column()
    is_primary: boolean
}

repositories/product-category.ts


import {
    dataSource,
} from "@medusajs/medusa/dist/loaders/database"
import {
    // alias the core repository to not cause a naming conflict
    ProductCategoryRepository as MedusaProductRepository,
} from "@medusajs/medusa/dist/repositories/product-category"
import { ProductCategory } from "../models/product-category"

export const ProductCategoryRepository = dataSource.getTreeRepository(ProductCategory).extend(
    Object.assign(MedusaProductRepository, { target: ProductCategory }),
);

export default ProductCategoryRepository;

loader.ts


export default async function () {

    const productCategoryImports = (await import('@medusajs/medusa/dist/api/routes/store/product-categories/index')) as any;
    productCategoryImports.defaultStoreProductCategoryFields = [...productCategoryImports.defaultStoreProductCategoryFields, 'is_primary'];
    productCategoryImports.allowedStoreProductsRelations = [...productCategoryImports.allowedStoreProductCategoryFields, 'is_primary'];

    const productCategoryAdminImports = (await import('@medusajs/medusa/dist/api/routes/admin/product-categories/index')) as any;
    productCategoryAdminImports.defaultStoreProductCategoryFields = [...productCategoryAdminImports.defaultStoreProductCategoryFields, 'is_primary'];
    productCategoryAdminImports.allowedStoreProductsRelations = [...productCategoryAdminImports.allowedStoreProductCategoryFields, 'is_primary'];
}

api/index.ts

import { registerOverriddenValidators } from "@medusajs/medusa"
import {
    AdminPostProductCategoriesReq as MedusaAdminPostProductCategoryReq
} from "@medusajs/medusa/dist/api/routes/admin/product-categories/create-product-category"
import { IsString, IsBoolean } from "class-validator"

class AdminPostProductCategoriesReq extends MedusaAdminPostProductCategoryReq {
    @IsBoolean()
    is_primary: boolean
}

registerOverriddenValidators(AdminPostProductCategoriesReq);
adrien2p commented 5 months ago

You need to redefine the parent category as it rely on product category as well.

veract-dev commented 5 months ago

Tried both ways below still same issue.

models/product-category.ts

import { Column, Entity, Relation, Tree } from "typeorm"
import {
    // alias the core entity to not cause a naming conflict
    ProductCategory as MedusaProductCategory,
} from "@medusajs/medusa"

@Entity()
@Tree("materialized-path")
export class ProductCategory extends MedusaProductCategory {
    @Column()
    is_primary: boolean

    parent_category: Relation<ProductCategory> | null;
}
import { Column, Entity, Relation, Tree } from "typeorm"
import {
    // alias the core entity to not cause a naming conflict
    ProductCategory as MedusaProductCategory,
} from "@medusajs/medusa"

@Entity()
@Tree("materialized-path")
export class ProductCategory extends MedusaProductCategory {
    @Column()
    is_primary: boolean
    @Column({ type: 'character varying', nullable: true })
    parent_category: Relation<ProductCategory> | null;
}
adrien2p commented 5 months ago

can you try with the following

  @TreeParent()
  @JoinColumn({ name: "parent_category_id" })
  parent_category: Relation<ProductCategory> | null

  // Typeorm also keeps track of the category's parent at all times.
  @Column()
  parent_category_id: string | null

  @TreeChildren({ cascade: true })
  category_children: Relation<ProductCategory>[]
veract-dev commented 5 months ago

Thank you it worked

adrien2p commented 5 months ago

Glad to ear :D