TypeOrm,静态表上的多对一关系

时间:2018-07-13 08:02:57

标签: typeorm

问题类型: [x]问题

数据库系统/驱动程序: [x] postgres

TypeORM版本: [x] 0.2.4

复制步骤或显示问题的小型存储库:

我在设置TypeOrm时遇到问题...在我的项目中,我需要访问一些SAP表,并且y无法生成用于关系的额外字段。

这是主要实体(简化),我需要创建四个关系(VBKD,VBEP,VBAP和VBPA)

var vbak = new EntitySchema({
name: 'VBAK',
tableName: 'VBAK',
skipSync: false,
columns: {
    VBELN: {
        type:"varchar",
        primary: true,
        generated: false,
    },
},
relations: {
    comercials: {
        target: "VBKD",
        type: "one-to-many",
        cascade: true,
        joinColumn: "VBELN",
    },
    positions: {
        target: "VBAP",
        type: "one-to-many",
        cascade: true,
        joinColumn: "VBELN",
    },
    interlocutors: {
        target: "VBPA",
        type: "one-to-many",
        cascade: true,
        joinColumn: "VBELN",
    },
    distributions: {
        target: "VBEP",
        type: "one-to-many",
        cascade: true,
        joinColumn: "VBELN",
    },
}});

这是可行的,关系名称为“空”,因为这是我发现避免字段自动创建的唯一方法。

var vbep = new EntitySchema({
    name: 'VBEP',
    tableName: 'VBEP',
    skipSync: true,
    columns: {
        VBELN: {
            type:"varchar",
            primary: true,
            generated: false,
        },
    },
    relations: {
        "": {
            target: 'VBAK',
            type: 'many-to-one',
            inverseSide: "distributions",
            joinColumn: "VBELN",
        },
    }
});

这让我头疼...看一下最终SQL。

var vbap = new EntitySchema({
    name: 'VBAP',
    tableName: 'VBAP',
    skipSync: false,
    columns: {
        VBELN: {
            type:"varchar",
            primary: true,
            generated: false,
        },
    },
    relations: {
        "": {
            target: 'VBAK',
            type: 'many-to-one',
            inverseSide: "positions",
            joinColumn: "VBELN"
        },
        "": {
            target: 'MARA',
            type: 'many-to-one',
            inverseSide: "orders",
            joinColumn: "MATNR",
        }
    }
});

这是启动的SQL

SELECT *
FROM "VBAK" "VBAK" 
LEFT JOIN "VBKD" "VBAK_comercials" ON "VBAK_comercials"."VBELN"="VBAK"."VBELN"  
LEFT JOIN "VBEP" "VBAK_distributions" ON "VBAK_distributions"."VBELN"="VBAK"."VBELN"  
LEFT JOIN "VBAP" "VBAK_positions" ON "VBAK_positions"."MATNR"=VBAK.MATNR  
LEFT JOIN "VBPA" "VBAK_interlocutors" ON "VBAK_interlocutors"."KUNNR"=VBAK.KUNNR;

如您所见,最后两个联接使用了错误的联接列,应该是

...LEFT JOIN "VBAP" "VBAK_positions" ON "VBAK_positions"."VBELN"=VBAK.VBELN  
LEFT JOIN "VBPA" "VBAK_interlocutors" ON "VBAK_interlocutors"."VBELN"=VBAK.VBELN;

我认为它使用的是第二个joincolumn而不是第一个。

还有另一种方法可以做我真正需要的吗? 谢谢。

0 个答案:

没有答案