diff --git a/src/drivers/AbstractDriver.ts b/src/drivers/AbstractDriver.ts index a086f00..07a08f3 100644 --- a/src/drivers/AbstractDriver.ts +++ b/src/drivers/AbstractDriver.ts @@ -369,11 +369,20 @@ export default abstract class AbstractDriver { ownerColumns[0].tscName, ownerEntity ); + + let fieldType = ""; + if (isOneToMany) { + fieldType = `${ownerColumns[0].tscType}[]`; + } else { + fieldType = ownerColumns[0].tscType; + if (ownerColumns[0].options.nullable) { + fieldType += " | null"; + } + } + ownerEntity.relationIds.push({ fieldName: relationIdFieldName, - fieldType: isOneToMany - ? `${ownerColumns[0].tscType}[]` - : ownerColumns[0].tscType, + fieldType, relationField: ownerRelation.fieldName }); // TODO: RelationId on ManyToMany diff --git a/src/drivers/MssqlDriver.ts b/src/drivers/MssqlDriver.ts index e760810..3e9e1ab 100644 --- a/src/drivers/MssqlDriver.ts +++ b/src/drivers/MssqlDriver.ts @@ -204,6 +204,7 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG tscType = "string"; break; default: + tscType = "NonNullable"; TomgUtils.LogError( `Unknown column type: ${resp.DATA_TYPE} table name: ${resp.TABLE_NAME} column name: ${resp.COLUMN_NAME}` ); @@ -230,17 +231,14 @@ WHERE TABLE_TYPE='BASE TABLE' and TABLE_SCHEMA in (${schema}) AND TABLE_CATALOG ? resp.CHARACTER_MAXIMUM_LENGTH : undefined; } - - if (columnType) { - ent.columns.push({ - generated, - type: columnType, - default: defaultValue, - options, - tscName, - tscType - }); - } + ent.columns.push({ + generated, + type: columnType, + default: defaultValue, + options, + tscName, + tscType + }); }); }); return entities; diff --git a/src/drivers/MysqlDriver.ts b/src/drivers/MysqlDriver.ts index f63212c..59a5e33 100644 --- a/src/drivers/MysqlDriver.ts +++ b/src/drivers/MysqlDriver.ts @@ -237,6 +237,7 @@ export default class MysqlDriver extends AbstractDriver { tscType = "string"; break; default: + tscType = "NonNullable"; TomgUtils.LogError( `Unknown column type: ${resp.DATA_TYPE} table name: ${resp.TABLE_NAME} column name: ${resp.COLUMN_NAME}` ); @@ -273,16 +274,14 @@ export default class MysqlDriver extends AbstractDriver { : undefined; } - if (columnType) { - ent.columns.push({ - generated, - type: columnType, - default: defaultValue, - options, - tscName, - tscType - }); - } + ent.columns.push({ + generated, + type: columnType, + default: defaultValue, + options, + tscName, + tscType + }); }); }); return entities; diff --git a/src/drivers/OracleDriver.ts b/src/drivers/OracleDriver.ts index f5f7c04..9518d54 100644 --- a/src/drivers/OracleDriver.ts +++ b/src/drivers/OracleDriver.ts @@ -187,6 +187,7 @@ export default class OracleDriver extends AbstractDriver { tscType = "number"; break; default: + tscType = "NonNullable"; TomgUtils.LogError( `Unknown column type:${DATA_TYPE}` ); @@ -211,16 +212,14 @@ export default class OracleDriver extends AbstractDriver { resp.DATA_LENGTH > 0 ? resp.DATA_LENGTH : undefined; } - if (columnType) { - ent.columns.push({ - generated, - type: columnType, - default: defaultValue, - options, - tscName, - tscType - }); - } + ent.columns.push({ + generated, + type: columnType, + default: defaultValue, + options, + tscName, + tscType + }); }); }); return entities; diff --git a/src/drivers/PostgresDriver.ts b/src/drivers/PostgresDriver.ts index 2703f59..cdd0796 100644 --- a/src/drivers/PostgresDriver.ts +++ b/src/drivers/PostgresDriver.ts @@ -111,21 +111,22 @@ export default class PostgresDriver extends AbstractDriver { resp.udt_name, resp.enumvalues ); - if (!columnTypes.sqlType || !columnTypes.tsType) { + if (columnTypes.tsType === "NonNullable") { if ( resp.data_type === "USER-DEFINED" || resp.data_type === "ARRAY" ) { TomgUtils.LogError( - `Unknown ${resp.data_type} column type: ${resp.udt_name} table name: ${resp.table_name} column name: ${resp.column_name}` + `Unknown ${resp.data_type} column type: ${resp.udt_name} table name: ${resp.table_name} column name: ${resp.column_name}` ); } else { TomgUtils.LogError( - `Unknown column type: ${resp.data_type} table name: ${resp.table_name} column name: ${resp.column_name}` + `Unknown column type: ${resp.data_type} table name: ${resp.table_name} column name: ${resp.column_name}` ); } return; } + const columnType = columnTypes.sqlType; let tscType = columnTypes.tsType; if (columnTypes.isArray) options.array = true; @@ -164,16 +165,15 @@ export default class PostgresDriver extends AbstractDriver { ? resp.character_maximum_length : undefined; } - if (columnType && tscType) { - ent.columns.push({ - generated, - type: columnType, - default: defaultValue, - options, - tscName, - tscType - }); - } + + ent.columns.push({ + generated, + type: columnType, + default: defaultValue, + options, + tscName, + tscType + }); }); }); return entities; @@ -185,17 +185,16 @@ export default class PostgresDriver extends AbstractDriver { enumValues: string | null ) { let ret: { - tsType?: Column["tscType"]; - sqlType: string | null; + tsType: Column["tscType"]; + sqlType: string; isArray: boolean; enumValues: string[]; } = { - tsType: undefined, - sqlType: null, + tsType: "", + sqlType: dataType, isArray: false, enumValues: [] }; - ret.sqlType = dataType; switch (dataType) { case "int2": ret.tsType = "number"; @@ -401,16 +400,12 @@ export default class PostgresDriver extends AbstractDriver { .join('" | "')}"` as never) as string; ret.sqlType = "enum"; ret.enumValues = enumValues.split(","); - } else { - ret.tsType = undefined; - ret.sqlType = null; } break; } break; default: - ret.tsType = undefined; - ret.sqlType = null; + ret.tsType = "NonNullable"; break; } return ret; diff --git a/src/drivers/SqliteDriver.ts b/src/drivers/SqliteDriver.ts index ac99030..2eee238 100644 --- a/src/drivers/SqliteDriver.ts +++ b/src/drivers/SqliteDriver.ts @@ -172,6 +172,7 @@ export default class SqliteDriver extends AbstractDriver { tscType = "Date"; break; default: + tscType = "NonNullable"; TomgUtils.LogError( `Unknown column type: ${columnType} table name: ${ent.tscName} column name: ${resp.name}` ); @@ -226,17 +227,15 @@ export default class SqliteDriver extends AbstractDriver { ); } - if (columnType) { - ent.columns.push({ - generated, - primary: isPrimary, - type: columnType, - default: defaultValue, - options, - tscName, - tscType - }); - } + ent.columns.push({ + generated, + primary: isPrimary, + type: columnType, + default: defaultValue, + options, + tscName, + tscType + }); }); }) ); diff --git a/src/templates/entity.mst b/src/templates/entity.mst index 2c2c1c9..12382ad 100644 --- a/src/templates/entity.mst +++ b/src/templates/entity.mst @@ -6,7 +6,7 @@ import {{localImport (toEntityName .)}} from './{{toFileName .}}' {{/inline}} {{#*inline "Column"}} {{#generated}}@PrimaryGeneratedColumn({ type:"{{type}}", {{/generated}}{{^generated}}@Column("{{type}}",{ {{#primary}}primary:{{primary}},{{/primary}}{{/generated}}{{json options}}{{#default}},default: {{.}},{{/default}} }) -{{printPropertyVisibility}}{{toPropertyName tscName}}{{strictMode}}:{{tscType}}; +{{printPropertyVisibility}}{{toPropertyName tscName}}{{strictMode}}:{{tscType}}{{#if options.nullable}} | null{{/if}}; {{/inline}} {{#*inline "JoinColumnOptions"}} diff --git a/test/drivers/MssqlDriver.test.ts b/test/drivers/MssqlDriver.test.ts index cff8cdc..19d90ff 100644 --- a/test/drivers/MssqlDriver.test.ts +++ b/test/drivers/MssqlDriver.test.ts @@ -74,7 +74,7 @@ describe("MssqlDriver", () => { COLUMN_DEFAULT: "'a'", COLUMN_NAME: "name", DATA_TYPE: "int", - IS_NULLABLE: "YES", + IS_NULLABLE: "NO", NUMERIC_PRECISION: 0, NUMERIC_SCALE: 0, IsIdentity: 1 @@ -99,7 +99,6 @@ describe("MssqlDriver", () => { const expected: Entity[] = JSON.parse(JSON.stringify(entities)); expected[0].columns.push({ options: { - nullable: true, name: "name" }, type: "int", diff --git a/test/integration/github-issues/227/entity/Post.ts b/test/integration/github-issues/227/entity/Post.ts new file mode 100644 index 0000000..20ce4c5 --- /dev/null +++ b/test/integration/github-issues/227/entity/Post.ts @@ -0,0 +1,15 @@ +import { PrimaryGeneratedColumn, Column, Entity, OneToOne, JoinColumn, Index } from "typeorm"; + +@Entity("Post") +export class Post { + + @PrimaryGeneratedColumn() + id: number; + + @Column("varchar", { nullable: true }) + title: string | null; + + @Column() + text: string; + +}