“无法读取未定义的属性 'tablePath'”(类型 ORM)

Posted

技术标签:

【中文标题】“无法读取未定义的属性 \'tablePath\'”(类型 ORM)【英文标题】:"Cannot read property 'tablePath' of undefined" ( TYPE ORM )“无法读取未定义的属性 'tablePath'”(类型 ORM) 【发布时间】:2020-05-25 05:03:33 【问题描述】:
    
        "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>)"
    

我在尝试使用映射表映射两个表时遇到此错误。

供应商模型(表 1)

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

供应商类别模型(表 2)

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

映射表(供应商到类别)

   @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[];

请帮我解决这个错误!

【问题讨论】:

【参考方案1】:

您的问题与@JoinColumn装饰器的错误使用有关;在处理@ManyToMany 关系时,您应该使用@JoinTable

所以,不要这样:

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

你应该写:

@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[];`

TypeORM 文档在处理关系时并没有那么具体,但它们确实提供了一些有用的示例。想知道详细答案的可以关注them。

【讨论】:

以上是关于“无法读取未定义的属性 'tablePath'”(类型 ORM)的主要内容,如果未能解决你的问题,请参考以下文章