首页 > 解决方案 > 多次指定 typeorm 表名

问题描述

我有下一个查询:

const foundDeal: any = await dealRepository.findOne({
  where: { id: dealId },
  relations: ['negotiationPointsDeals', 'chosenInventoryToSubtractQuantity',


    'chosenInventoryToSubtractQuantity.inventoryItemType',
    'chosenInventoryToSubtractQuantity.inventoryItemType.quality', 'negotiationPointsDeals.negotiationPointsTemplate',
    'chosenInventoryToSubtractQuantity.addressOfOriginId', 'chosenInventoryToSubtractQuantity.currentLocationAddress',


    'chosenInventoryToSubtractQuantity.labAttestationDocs',
    'chosenInventoryToSubtractQuantity.labAttestationDocs.storage',

    'chosenInventoryToSubtractQuantity.proveDocuments', 'chosenInventoryToSubtractQuantity.proveDocuments.storage',
    'chosenInventoryToSubtractQuantity.inventoryItemSavedFields', 'chosenInventoryToSubtractQuantity.inventoryItemSavedFields.proveDocuments',
    'chosenInventoryToSubtractQuantity.inventoryItemSavedFields.proveDocuments.storage',


    'sellerBroker', 'sellerBroker.users',
    'seller', 'seller.users',
    'buyerBroker', 'buyerBroker.users',
    'buyer', 'buyer.users',
    'order', 'order.inventory', 'order.inventory.inventoryItemType',
    'order.inventory.inventoryItemType.quality',
    'order.inventory.addressOfOriginId', 'order.inventory.currentLocationAddress',
    'order.inventory.inventoryItemSavedFields', 'order.inventory.inventoryItemSavedFields.proveDocuments',
    'order.inventory.inventoryItemSavedFields.proveDocuments.storage',

    'order.inventory.labAttestationDocs', 'order.inventory.labAttestationDocs.storage',

    // 'postTradeProcessingDeal', 'postTradeProcessingDeal.postTradeProcessingStepsDeal',

    'order.inventory.proveDocuments',
    'order.inventory.proveDocuments.storage',
    'negotiationPointsDeals.negotiationPointsTemplate.negotiationPointsTemplateChoices',
    'postTradeProcessing',
  ],
});

所以,错误是下一个:

错误:多次指定表名“Deal__chosenInventoryToSubtractQuantity_Deal__chosenInventoryTo”。

但我在查询中看不到任何双打。

标签: typeorm

解决方案


此 TypeORM 问题中记录了该问题。

经过一番挖掘,我意识到这个错误是由于 TypeORM 在使用比 Postgres 名称限制长的预先加载时创建了某种变量。

例如,如果您急切地加载带有 customer 的产品,typeorm 将创建一些类似于 customer_products 的内容,将两者连接起来。如果该名称超过 63 个字节(Postgres 限制),则查询将崩溃。

基本上,当您的变量名太长或嵌套过多时,就会发生这种情况。使您的实体名称更短,它将起作用。否则,您可以使用手动连接表queryBuilder并为其分配别名。


推荐阅读