fix generation of complex manyToMany relationships

fix generation of multiple relations based on the same column
This commit is contained in:
Kononnable 2019-11-08 23:57:01 +01:00
parent 1057b665ac
commit 2cc6b4f43c
10 changed files with 42 additions and 23 deletions

View File

@ -21,11 +21,20 @@ export default function modelCustomizationPhase(
} else {
namingStrategy = new NamingStrategy();
}
let retVal = applyNamingStrategy(namingStrategy, dbModel);
let retVal = removeColumnsInRelation(dbModel);
retVal = applyNamingStrategy(namingStrategy, dbModel);
retVal = addImportsAndGenerationOptions(retVal, generationOptions);
retVal = removeColumnDefaultProperties(retVal, defaultValues);
return retVal;
}
function removeColumnsInRelation(dbModel: Entity[]): Entity[] {
dbModel.forEach(entity => {
entity.columns = entity.columns.filter(
col => !col.isUsedInRelation || col.primary
);
});
return dbModel;
}
function removeColumnDefaultProperties(
dbModel: Entity[],
defaultValues: DataTypeDefaults

View File

@ -88,6 +88,8 @@ export default abstract class AbstractDriver {
) &&
entity.relations[0].relatedTable !==
entity.relations[1].relatedTable &&
entity.relations[0].joinColumnOptions!.length ===
entity.relations[1].joinColumnOptions!.length &&
entity.columns.length ===
entity.columns.filter(c => c.primary).length &&
entity.columns
@ -315,16 +317,12 @@ export default abstract class AbstractDriver {
);
isOneToMany = !index;
ownerEntity.columns = ownerEntity.columns.filter(
v =>
!relationTmp.ownerColumns.some(u => u === v.tscName) ||
v.primary
);
relationTmp.relatedTable.columns = relationTmp.relatedTable.columns.filter(
v =>
!relationTmp.relatedColumns.some(u => u === v.tscName) ||
v.primary
);
ownerColumns.forEach(column => {
column.isUsedInRelation = true;
});
relatedColumns.forEach(column => {
column.isUsedInRelation = true;
});
let fieldName = "";
if (ownerColumns.length === 1) {
fieldName = TomgUtils.findNameForNewField(

View File

@ -226,7 +226,8 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
default: defaultValue,
options,
tscName,
tscType
tscType,
isUsedInRelation: false
});
}
});

View File

@ -257,7 +257,8 @@ export default class MysqlDriver extends AbstractDriver {
default: defaultValue,
options,
tscName,
tscType
tscType,
isUsedInRelation: false
});
}
});

View File

@ -208,7 +208,8 @@ export default class OracleDriver extends AbstractDriver {
default: defaultValue,
options,
tscName,
tscType
tscType,
isUsedInRelation: false
});
}
});

View File

@ -159,7 +159,8 @@ export default class PostgresDriver extends AbstractDriver {
default: defaultValue,
options,
tscName,
tscType
tscType,
isUsedInRelation: false
});
}
});

View File

@ -226,7 +226,8 @@ export default class SqliteDriver extends AbstractDriver {
default: defaultValue,
options,
tscName,
tscType
tscType,
isUsedInRelation: false
});
}
});

View File

@ -3,7 +3,8 @@ import { ColumnType } from "typeorm";
export type Column = {
tscType: string;
tscName: string;
type: ColumnType | string; // todo: remove ?
type: ColumnType | string; // TODO: remove ?
isUsedInRelation: boolean; // TODO: move to separate object/calulate when us
primary?: boolean;
generated?: true | "increment" | "uuid";

View File

@ -106,7 +106,8 @@ describe("MssqlDriver", () => {
generated: true,
default: `() => "'a'"`,
tscName: "name",
tscType: "number"
tscType: "number",
isUsedInRelation: false
});
const result = await driver.GetCoulmnsFromEntity(

View File

@ -21,13 +21,15 @@ describe("Model customization phase", async () => {
options: { name: "id" },
tscName: "id",
tscType: "number",
primary: true
primary: true,
isUsedInRelation: false
},
{
type: "character varying",
options: { name: "name" },
tscName: "name",
tscType: "string"
tscType: "string",
isUsedInRelation: false
}
],
indices: [
@ -61,19 +63,22 @@ describe("Model customization phase", async () => {
options: { name: "id" },
tscName: "id",
tscType: "number",
primary: true
primary: true,
isUsedInRelation: false
},
{
type: "character varying",
options: { name: "title" },
tscName: "title",
tscType: "string"
tscType: "string",
isUsedInRelation: false
},
{
type: "character varying",
options: { name: "text" },
tscName: "text",
tscType: "string"
tscType: "string",
isUsedInRelation: false
}
],
indices: [