fix removal of columns used in relations which where also used in indices
This commit is contained in:
parent
beaf3a09da
commit
5cdf4691f4
@ -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
|
||||
);
|
||||
});
|
||||
|
@ -348,7 +348,7 @@ export default abstract class AbstractDriver {
|
||||
relationTmp.relatedTable
|
||||
),
|
||||
joinColumnOptions: relationTmp.ownerColumns.map((v, idx) => {
|
||||
const retVal: JoinColumnOptions = {
|
||||
const retVal: Required<JoinColumnOptions> = {
|
||||
name: v,
|
||||
referencedColumnName: relationTmp.relatedColumns[idx]
|
||||
};
|
||||
|
@ -7,6 +7,6 @@ export type Relation = {
|
||||
relatedField: string;
|
||||
fieldName: string;
|
||||
relationOptions?: RelationOptions;
|
||||
joinColumnOptions?: JoinColumnOptions[];
|
||||
joinColumnOptions?: Required<JoinColumnOptions>[];
|
||||
joinTableOptions?: JoinTableMultipleColumnsOptions;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user