fix removal of some of columns from relations

proper referencedColumnName value
This commit is contained in:
Kononnable 2019-11-10 14:54:48 +01:00
parent 396a461626
commit beaf3a09da
11 changed files with 34 additions and 41 deletions

View File

@ -30,7 +30,10 @@ export default function modelCustomizationPhase(
function removeColumnsInRelation(dbModel: Entity[]): Entity[] {
dbModel.forEach(entity => {
entity.columns = entity.columns.filter(
col => !col.isUsedInRelation || col.primary
col =>
!col.isUsedInRelationAsOwner ||
col.isUsedInRelationAsReferenced ||
col.primary
);
});
return dbModel;

View File

@ -318,10 +318,10 @@ export default abstract class AbstractDriver {
isOneToMany = !index;
ownerColumns.forEach(column => {
column.isUsedInRelation = true;
column.isUsedInRelationAsOwner = true;
});
relatedColumns.forEach(column => {
column.isUsedInRelation = true;
column.isUsedInRelationAsReferenced = true;
});
let fieldName = "";
if (ownerColumns.length === 1) {

View File

@ -57,22 +57,22 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
IsIdentity: number;
IsUnique: number;
}[] = (await request.query(`SELECT TABLE_NAME,COLUMN_NAME,COLUMN_DEFAULT,IS_NULLABLE,
DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') IsIdentity,
(SELECT count(*)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
on cu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
where
tc.CONSTRAINT_TYPE = 'UNIQUE'
and tc.TABLE_NAME = c.TABLE_NAME
and cu.COLUMN_NAME = c.COLUMN_NAME
and tc.TABLE_SCHEMA=c.TABLE_SCHEMA) IsUnique
FROM INFORMATION_SCHEMA.COLUMNS c
where TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG in (${MssqlDriver.escapeCommaSeparatedList(
DATA_TYPE,CHARACTER_MAXIMUM_LENGTH,NUMERIC_PRECISION,NUMERIC_SCALE,
COLUMNPROPERTY(object_id(TABLE_NAME), COLUMN_NAME, 'IsIdentity') IsIdentity,
(SELECT count(*)
FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
inner join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE cu
on cu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
where
tc.CONSTRAINT_TYPE = 'UNIQUE'
and tc.TABLE_NAME = c.TABLE_NAME
and cu.COLUMN_NAME = c.COLUMN_NAME
and tc.TABLE_SCHEMA=c.TABLE_SCHEMA) IsUnique
FROM INFORMATION_SCHEMA.COLUMNS c
where TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG in (${MssqlDriver.escapeCommaSeparatedList(
dbNames
)})
order by ordinal_position`)).recordset;
order by ordinal_position`)).recordset;
entities.forEach(ent => {
response
.filter(filterVal => {
@ -226,8 +226,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG
default: defaultValue,
options,
tscName,
tscType,
isUsedInRelation: false
tscType
});
}
});

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -11,7 +11,7 @@ import { {{toEntityName .}} } from './{{toFileName .}}'
{{/inline}}
{{#*inline "Relation"}}
@{{relationType}}(()=>{{toEntityName relatedTable}},{{toEntityName relatedTable}}=>{{toEntityName relatedTable}}.{{toPropertyName relatedField}}{{#if relationOptions}},{ {{json relationOptions}} }{{/if}})
{{#if joinColumnOptions}}@JoinColumn([{{json joinColumnOptions}}]){{/if}}
{{#if joinColumnOptions}}@JoinColumn([{{#joinColumnOptions}}{ name: "{{name}}", referencedColumnName: "{{toPropertyName referencedColumnName}}" },{{/joinColumnOptions}}]){{/if}}
{{#if joinTableOptions}}@JoinTable({ {{json joinTableOptions}} }){{/if}}
{{printPropertyVisibility}}{{toPropertyName fieldName}}{{strictMode}}:{{toRelation (toEntityName relatedTable) relationType}};

View File

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

View File

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