Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Cannot read property 'tablePath' of undefined" ( TYPE ORM )

    {
        "name": "TypeError"
"message": "Cannot read property 'tablePath' of undefined",
        "stack": "TypeError: Cannot read property 'tablePath' of undefined\n    at C:\\working\\Hianfa\\Hianfa\\api\\src\\query-builder\\SelectQueryBuilder.ts:1500:76\n    at Array.map (<anonymous>)\n    at SelectQueryBuilder.createJoinExpression (C:\\working\\Hianfa\\Hianfa\\api\\src\\query-builder\\SelectQueryBuilder.ts:1462:57)\n    at SelectQueryBuilder.getQuery (C:\\working\\Hianfa\\Hianfa\\api\\src\\query-builder\\SelectQueryBuilder.ts:54:21)\n    at SelectQueryBuilder.QueryBuilder.getQueryAndParameters (C:\\working\\Hianfa\\Hianfa\\api\\src\\query-builder\\QueryBuilder.ts:392:28)\n    at SelectQueryBuilder.<anonymous> (C:\\working\\Hianfa\\Hianfa\\api\\src\\query-builder\\SelectQueryBuilder.ts:1948:40)\n    at step (C:\\working\\Hianfa\\Hianfa\\api\\node_modules\\tslib\\tslib.js:136:27)\n    at Object.next (C:\\working\\Hianfa\\Hianfa\\api\\node_modules\\tslib\\tslib.js:117:57)\n    at C:\\working\\Hianfa\\Hianfa\\api\\node_modules\\tslib\\tslib.js:110:75\n    at new Promise (<anonymous>)"
    }

I am getting this error, while trying to map two tables with the mapping table.

VENDOR MODEL (TABLE 1)

@ManyToMany(type => VendorToCategory, vendorToCategory => vendorToCategory.vendor)
// @JoinColumn({name: 'vendor_vendor_category_id'})
public vendorToCategory: VendorToCategory[];

VENDOR CATEGORY MODEL (TABLE 2)

 @ManyToMany(type => VendorToCategory, vendorToCategory => vendorToCategory.vendorCategory)
    @JoinColumn({name: 'vendor_vendor_category_id'})
    public vendorToCategory: VendorToCategory[];

MAPPING TABLE (VENDOR TO CATEGORY)

   @ManyToMany(type => Vendor, vendor => vendor.vendorToCategory)
    @JoinColumn({ name: 'vendor_id' })
    public vendor: Vendor[];

@ManyToMany(type => VendorCategory, vendorCategory => vendorCategory.vendorToCategory)
@JoinColumn({ name: 'vendor_category_id' })
public vendorCategory: VendorCategory[];

Please help me out with this error!

like image 301
Prabhjyot Gambhir Avatar asked Feb 09 '20 20:02

Prabhjyot Gambhir


1 Answers

Your problem is related to the wrong use of the @JoinColumn decorator; you should use @JoinTable while working with @ManyToMany relationships.

So, instead of this:

@ManyToMany(type => VendorToCategory, vendorToCategory => 
vendorToCategory.vendorCategory)
@JoinColumn({name: 'vendor_vendor_category_id'})
public vendorToCategory: VendorToCategory[];`

You should write:

@ManyToMany(type => VendorToCategory, vendorToCategory => vendorToCategory.vendorCategory)
@JoinTable({
   name: "vendor_vendor_category_id",
   joinColumn: {
    name: "vendorCategory",
    referencedColumnName: "id"
   },
   inverseJoinColumn: {
    name: "vendor",
    referencedColumnName: "id"
    }})
public vendor: Vendor[];`

The TypeORM docs aren't that specific when it comes to deal with relations, but they do provide some useful examples. So take a look at them if you want a detailed answer.

like image 200
jmj0502 Avatar answered Nov 15 '22 15:11

jmj0502