From 5cdf4691f435cc4b8966092574137bb4564b8b03 Mon Sep 17 00:00:00 2001 From: Kononnable Date: Sun, 10 Nov 2019 16:23:32 +0100 Subject: [PATCH] fix removal of columns used in relations which where also used in indices --- src/ModelCustomization.ts | 34 +++++++++++++++++++++++++++++++++- src/drivers/AbstractDriver.ts | 2 +- src/models/Relation.ts | 2 +- 3 files changed, 35 insertions(+), 3 deletions(-) diff --git a/src/ModelCustomization.ts b/src/ModelCustomization.ts index d635bb3..6fbd896 100644 --- a/src/ModelCustomization.ts +++ b/src/ModelCustomization.ts @@ -1,4 +1,5 @@ import { DataTypeDefaults } from "typeorm/driver/types/DataTypeDefaults"; +import { DefaultNamingStrategy } from "typeorm/naming-strategy/DefaultNamingStrategy"; import { Entity } from "./models/Entity"; import IGenerationOptions from "./IGenerationOptions"; import AbstractNamingStrategy from "./AbstractNamingStrategy"; @@ -21,18 +22,49 @@ export default function modelCustomizationPhase( } else { namingStrategy = new NamingStrategy(); } - let retVal = removeColumnsInRelation(dbModel); + let retVal = removeIndicesGeneratedByTypeorm(dbModel); + retVal = removeColumnsInRelation(dbModel); retVal = applyNamingStrategy(namingStrategy, dbModel); retVal = addImportsAndGenerationOptions(retVal, generationOptions); retVal = removeColumnDefaultProperties(retVal, defaultValues); return retVal; } +function removeIndicesGeneratedByTypeorm(dbModel: Entity[]): Entity[] { + // TODO: Support typeorm CustomNamingStrategy + // TODO: PK index - ignores primaryKeyName(typeorm bug?) - to investigate + const namingStrategy = new DefaultNamingStrategy(); + dbModel.forEach(entity => { + entity.indices = entity.indices.filter( + v => !v.name.startsWith(`sqlite_autoindex_`) + ); + entity.relations + .filter(v => v.joinColumnOptions) + .forEach(rel => { + const columnNames = rel.joinColumnOptions!.map(v => v.name); + const idxName = namingStrategy.relationConstraintName( + entity.tscName, + columnNames + ); + const fkName = namingStrategy.foreignKeyName( + entity.tscName, + columnNames + ); + entity.indices = entity.indices.filter( + v => v.name !== idxName && v.name !== fkName + ); + }); + }); + return dbModel; +} function removeColumnsInRelation(dbModel: Entity[]): Entity[] { dbModel.forEach(entity => { entity.columns = entity.columns.filter( col => !col.isUsedInRelationAsOwner || col.isUsedInRelationAsReferenced || + entity.indices.some(idx => + idx.columns.some(v => v === col.tscName) + ) || col.primary ); }); diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index cbfe754..7682d41 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -348,7 +348,7 @@ export default abstract class AbstractDriver { relationTmp.relatedTable ), joinColumnOptions: relationTmp.ownerColumns.map((v, idx) => { - const retVal: JoinColumnOptions = { + const retVal: Required = { name: v, referencedColumnName: relationTmp.relatedColumns[idx] }; diff --git a/src/models/Relation.ts b/src/models/Relation.ts index c8cb02f..7c35c24 100644 --- a/src/models/Relation.ts +++ b/src/models/Relation.ts @@ -7,6 +7,6 @@ export type Relation = { relatedField: string; fieldName: string; relationOptions?: RelationOptions; - joinColumnOptions?: JoinColumnOptions[]; + joinColumnOptions?: Required[]; joinTableOptions?: JoinTableMultipleColumnsOptions; };