Skip to content

docs: add column type inference explanation - #12317

Open
ElwinErnst wants to merge 4 commits into
typeorm:masterfrom
ElwinErnst:docs/inferred-column-types
Open

ElwinErnst wants to merge 4 commits into
typeorm:masterfrom
ElwinErnst:docs/inferred-column-types

Conversation

@ElwinErnst

@ElwinErnst ElwinErnst commented Apr 7, 2026 •

Copy link
Copy Markdown

Description of change

This PR adds documentation explaining how TypeORM infers column types when no explicit type is provided in the @Column() decorator.

Currently, this behavior is not clearly documented, which can lead to confusion for developers when defining entities without specifying column types.

What was added

  • Explanation of column type inference behavior
  • Example using @Column() without explicit type
  • Mapping table for common TypeScript types across database drivers
  • Notes about driver-specific behavior and limitations
  • Clarification on what to do when type inference is not possible

Why this change is needed

The current documentation does not explain how TypeORM determines column types when the type option is omitted. This PR provides a clear and practical overview to help developers understand the behavior and avoid unexpected results.

How it was verified

  • Reviewed existing documentation to confirm the gap
  • Cross-checked behavior with TypeORM driver implementations
  • Ensured the content is consistent with driver-specific variability

Pull-Request Checklist

  • Code is up-to-date with the master branch
  • This pull request links relevant issues as Fixes #8827
  • There are new or updated tests validating the change (N/A - documentation only)
  • Documentation has been updated to reflect this change

Fixes #8827

@qodo-free-for-open-source-projects

qodo-free-for-open-source-projects Bot commented Apr 7, 2026 •

Copy link
Copy Markdown

Code Review by Qodo

🐞 Bugs (0)   📘 Rule violations (0)   📎 Requirement gaps (2)   🎨 UX Issues (0)
📎\ ≡ Correctness (1) ⚙ Maintainability (1) ⭐ New (1)

Grey Divider


Action required

1. Inference table misses common types 📎 ≡ ⭐
Description
The new mapping table only lists string, number, boolean, and Date, so it does not cover
common TypeScript/entity property types (or explicitly state scope/omissions). This leaves users
unable to predict inferred column types for many typical fields.
Code

docs/docs/entity/1-entities.md[R229-235]

+| TypeScript Type | PostgreSQL | MySQL / MariaDB | SQLite | SQL Server |
+|----------------|------------|-----------------|--------|------------|
+| `string` | `varchar` / `text` | `varchar` | `text` | `nvarchar` |
+| `number` | `int` | `int` | `integer` | `int` |
+| `boolean` | `boolean` | `tinyint` | `boolean` | `bit` |
+| `Date` | `timestamp` | `datetime` | `datetime` | `datetime` |
+
Evidence
PR Compliance ID 6 requires a discoverable mapping table covering all common TypeScript types (or
clearly stating scope/omissions). The added table includes only four TypeScript types and does not
clearly define scope/omissions beyond saying it is a set of "common mappings".

Provide a mapping table for TypeScript types to inferred database column types
docs/docs/entity/1-entities.md[229-235]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The TypeScript-to-column-type inference table is incomplete for common entity property types and does not clearly state its scope/omissions.

## Issue Context
The compliance checklist requires a mapping table that covers all common TypeScript types supported by TypeORM (or clearly states what is and is not covered).

## Fix Focus Areas
- docs/docs/entity/1-entities.md[229-243]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Inferred string defaults missing 📎 ⚙
Description
The new Column Type Inference section does not state key defaults required by the checklist, such as
the default varchar length (where applicable) and when string becomes varchar vs text per
driver. This can mislead users and fails the required documentation completeness for inferred column
types.
Code

docs/docs/entity/1-entities.md[R224-240]

+The inferred type depends on the database driver and its internal implementation.
+
+| TypeScript Type | PostgreSQL | MySQL / MariaDB | SQLite | SQL Server |
+|----------------|------------|-----------------|--------|------------|
+| `string` | `varchar` / `text` | `varchar` | `text` | `nvarchar` |
+| `number` | `int` | `int` | `integer` | `int` |
+| `boolean` | `boolean` | `tinyint` | `boolean` | `bit` |
+| `Date` | `timestamp` | `datetime` | `datetime` | `datetime` |
+
+> **Note:**
+>
+> - The exact mapping is determined internally by each database driver implementation.
+> - The actual column type may vary depending on configuration and driver-specific behavior.
+> - In some databases, types like `varchar` without length behave similarly to `text`.
+> - If TypeORM cannot infer a suitable database column type from the TypeScript property type, you must explicitly specify the column type in `@Column()`.
+> - For production systems, it is recommended to explicitly define column types.
+
Evidence
PR Compliance ID 5 requires documenting inferred column types with key defaults (including string
mapping details and default varchar length). The added table lists types but omits default lengths
and does not clarify the conditions under which string maps to varchar vs text per driver.

Document inferred TypeORM column types when @Column() has no explicit type
docs/docs/entity/1-entities.md[224-240]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new documentation section on inferred column types is missing checklist-required details, specifically:
- the default `varchar` length (where applicable), and
- clearer driver-specific conditions for when `string` becomes `varchar` vs `text`.
## Issue Context
PR Compliance ID 5 requires these defaults/clarifications to avoid user confusion when `@Column()` omits an explicit `type`.
## Fix Focus Areas
- docs/docs/entity/1-entities.md[224-240]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


3. Missing closing ``` in enum ☑ 📘 ⚙
Description
A closing Markdown code fence was removed, causing the subsequent ### \simple-array\`` section to
be rendered inside the previous TypeScript code block. This breaks documentation formatting and
violates the requirement to avoid inconsistent/sloppy style changes.
Code

docs/docs/entity/1-entities.md[R295-300]

})
role: UserRoleType
}
-```
### `simple-array` column type
Evidence
PR Compliance ID 4 forbids inconsistent style/noise in changes. In the modified enum example, the
closing code fence is deleted, and the next section header follows immediately, indicating an
unclosed code block introduced by this PR.

Rule 4: Remove AI-generated noise
docs/docs/entity/1-entities.md[295-300]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The enum example code block is not properly closed because the terminating triple-backtick fence was removed. This causes subsequent documentation sections to render as code.
## Issue Context
The `### `simple-array` column type` header should not be inside a code block.
## Fix Focus Areas
- docs/docs/entity/1-entities.md[295-300]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

4. Wrong inference type table ☑ 🐞 ≡
Description
The new column type inference table documents mappings that don’t match TypeORM’s current driver
normalizeType behavior (notably for PostgreSQL and SQLite), which can mislead users relying on the
table.
Code

docs/docs/entity/1-entities.md[R226-231]

+| TypeScript Type | PostgreSQL | MySQL / MariaDB | SQLite | SQL Server |
+|----------------|------------|-----------------|--------|------------|
+| `string` | `varchar` / `text` | `varchar` | `text` | `nvarchar` |
+| `number` | `int` | `int` | `integer` | `int` |
+| `boolean` | `boolean` | `tinyint` | `boolean` | `bit` |
+| `Date` | `timestamp` | `datetime` | `datetime` | `datetime` |
Evidence
Type inference uses Reflect.getMetadata('design:type') to set options.type to a JS constructor
(String/Number/Date/Boolean), then each driver maps that to a DB type string. The docs table claims
e.g. PostgreSQL number -> int and SQLite string -> text, but the drivers normalize
Number->integer and SQLite String->varchar (and PostgreSQL String->character varying,
Date->timestamp without time zone).

docs/docs/entity/1-entities.md[215-232]
src/decorator/columns/Column.ts[169-227]
src/driver/postgres/PostgresDriver.ts[1152-1179]
src/driver/sqlite-abstract/AbstractSqliteDriver.ts[653-677]
src/driver/mysql/MysqlDriver.ts[745-766]
src/driver/sqlserver/SqlServerDriver.ts[679-706]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new inference mapping table in the docs does not match the actual `normalizeType` results used by drivers when `@Column()` omits an explicit `type`.
### Issue Context
Type inference sets `options.type` from `Reflect.getMetadata('design:type')` (String/Number/Date/Boolean), and then each driver normalizes it. Several documented outputs differ from the driver implementations.
### Fix Focus Areas
- docs/docs/entity/1-entities.md[215-232]
- src/decorator/columns/Column.ts[169-227]
- src/driver/postgres/PostgresDriver.ts[1152-1179]
- src/driver/sqlite-abstract/AbstractSqliteDriver.ts[653-677]
- src/driver/mysql/MysqlDriver.ts[745-766]
- src/driver/sqlserver/SqlServerDriver.ts[679-706]
### Proposed fix
Update the table to reflect the actual normalized types, e.g.:
- PostgreSQL: `string` -> `character varying`, `number` -> `integer`, `Date` -> `timestamp without time zone`
- SQLite: `string` -> `varchar`
…and adjust wording to clarify these are `normalizeType` outputs (and may be equivalent/aliased in some DBs).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


5. Inference prerequisites missing ☑ 🐞 ≡
Description
The new section says TypeORM automatically infers types when @Column() omits type, but inference
depends on Reflect metadata; without reflect-metadata and emitDecoratorMetadata, TypeORM throws
ColumnTypeUndefinedError.
Code

docs/docs/entity/1-entities.md[R217-218]

+When no explicit type is provided in the `@Column()` decorator, TypeORM automatically infers the database column type based on the TypeScript property type.
+
Evidence
@Column() only guesses a type when Reflect.getMetadata('design:type', ...) is available;
otherwise, if no explicit options.type is set, it throws ColumnTypeUndefinedError, whose message
explicitly calls out emitDecoratorMetadata and importing reflect-metadata.

docs/docs/entity/1-entities.md[215-224]
src/decorator/columns/Column.ts[190-228]
src/error/ColumnTypeUndefinedError.ts[1-15]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new docs imply inference always works when `@Column()` omits `type`, but inference fails (and TypeORM throws) if Reflect metadata isn’t available.
### Issue Context
`@Column()` uses `Reflect.getMetadata('design:type')` to infer the type. When that metadata is missing, `ColumnTypeUndefinedError` is thrown, with guidance about `emitDecoratorMetadata` and importing `reflect-metadata`.
### Fix Focus Areas
- docs/docs/entity/1-entities.md[215-224]
- src/decorator/columns/Column.ts[190-228]
- src/error/ColumnTypeUndefinedError.ts[1-15]
### Proposed fix
Add a short note under “Column Type Inference” stating that inference requires `reflect-metadata` to be imported and `emitDecoratorMetadata: true` (TypeScript), and that otherwise you must specify `type` explicitly (or JS users must always specify it).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

ⓘ The new review experience is currently in Beta. Learn more

Grey Divider

Grey Divider

Previous review results

Review updated until commit bb1c65c

Results up to commit N/A


🐞 Bugs (0)  
📘 Rule violations (0)  
📎 Requirement gaps (1)  
🎨 UX Issues (0)

📎\ ⚙ Maintainability (1)

Grey Divider


Action required

1. Inferred string defaults missing 📎 ⚙
Description
The new Column Type Inference section does not state key defaults required by the checklist, such as
the default varchar length (where applicable) and when string becomes varchar vs text per
driver. This can mislead users and fails the required documentation completeness for inferred column
types.
Code

docs/docs/entity/1-entities.md[R224-240]

+The inferred type depends on the database driver and its internal implementation.
+
+| TypeScript Type | PostgreSQL | MySQL / MariaDB | SQLite | SQL Server |
+|----------------|------------|-----------------|--------|------------|
+| `string` | `varchar` / `text` | `varchar` | `text` | `nvarchar` |
+| `number` | `int` | `int` | `integer` | `int` |
+| `boolean` | `boolean` | `tinyint` | `boolean` | `bit` |
+| `Date` | `timestamp` | `datetime` | `datetime` | `datetime` |
+
+> **Note:**
+>
+> - The exact mapping is determined internally by each database driver implementation.
+> - The actual column type may vary depending on configuration and driver-specific behavior.
+> - In some databases, types like `varchar` without length behave similarly to `text`.
+> - If TypeORM cannot infer a suitable database column type from the TypeScript property type, you must explicitly specify the column type in `@Column()`.
+> - For production systems, it is recommended to explicitly define column types.
+
Evidence
PR Compliance ID 5 requires documenting inferred column types with key defaults (including string
mapping details and default varchar length). The added table lists types but omits default lengths
and does not clarify the conditions under which string maps to varchar vs text per driver.

Document inferred TypeORM column types when @Column() has no explicit type
docs/docs/entity/1-entities.md[224-240]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new documentation section on inferred column types is missing checklist-required details, specifically:
- the default `varchar` length (where applicable), and
- clearer driver-specific conditions for when `string` becomes `varchar` vs `text`.
## Issue Context
PR Compliance ID 5 requires these defaults/clarifications to avoid user confusion when `@Column()` omits an explicit `type`.
## Fix Focus Areas
- docs/docs/entity/1-entities.md[224-240]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


2. Missing closing ``` in enum ☑ 📘 ⚙
Description
A closing Markdown code fence was removed, causing the subsequent ### \simple-array\`` section to
be rendered inside the previous TypeScript code block. This breaks documentation formatting and
violates the requirement to avoid inconsistent/sloppy style changes.
Code

docs/docs/entity/1-entities.md[R295-300]

})
role: UserRoleType
}
-```
### `simple-array` column type
Evidence
PR Compliance ID 4 forbids inconsistent style/noise in changes. In the modified enum example, the
closing code fence is deleted, and the next section header follows immediately, indicating an
unclosed code block introduced by this PR.

Rule 4: Remove AI-generated noise
docs/docs/entity/1-entities.md[295-300]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The enum example code block is not properly closed because the terminating triple-backtick fence was removed. This causes subsequent documentation sections to render as code.
## Issue Context
The `### `simple-array` column type` header should not be inside a code block.
## Fix Focus Areas
- docs/docs/entity/1-entities.md[295-300]

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools



Remediation recommended

3. Wrong inference type table ☑ 🐞 ≡
Description
The new column type inference table documents mappings that don’t match TypeORM’s current driver
normalizeType behavior (notably for PostgreSQL and SQLite), which can mislead users relying on the
table.
Code

docs/docs/entity/1-entities.md[R226-231]

+| TypeScript Type | PostgreSQL | MySQL / MariaDB | SQLite | SQL Server |
+|----------------|------------|-----------------|--------|------------|
+| `string` | `varchar` / `text` | `varchar` | `text` | `nvarchar` |
+| `number` | `int` | `int` | `integer` | `int` |
+| `boolean` | `boolean` | `tinyint` | `boolean` | `bit` |
+| `Date` | `timestamp` | `datetime` | `datetime` | `datetime` |
Evidence
Type inference uses Reflect.getMetadata('design:type') to set options.type to a JS constructor
(String/Number/Date/Boolean), then each driver maps that to a DB type string. The docs table claims
e.g. PostgreSQL number -> int and SQLite string -> text, but the drivers normalize
Number->integer and SQLite String->varchar (and PostgreSQL String->character varying,
Date->timestamp without time zone).

docs/docs/entity/1-entities.md[215-232]
src/decorator/columns/Column.ts[169-227]
src/driver/postgres/PostgresDriver.ts[1152-1179]
src/driver/sqlite-abstract/AbstractSqliteDriver.ts[653-677]
src/driver/mysql/MysqlDriver.ts[745-766]
src/driver/sqlserver/SqlServerDriver.ts[679-706]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new inference mapping table in the docs does not match the actual `normalizeType` results used by drivers when `@Column()` omits an explicit `type`.
### Issue Context
Type inference sets `options.type` from `Reflect.getMetadata('design:type')` (String/Number/Date/Boolean), and then each driver normalizes it. Several documented outputs differ from the driver implementations.
### Fix Focus Areas
- docs/docs/entity/1-entities.md[215-232]
- src/decorator/columns/Column.ts[169-227]
- src/driver/postgres/PostgresDriver.ts[1152-1179]
- src/driver/sqlite-abstract/AbstractSqliteDriver.ts[653-677]
- src/driver/mysql/MysqlDriver.ts[745-766]
- src/driver/sqlserver/SqlServerDriver.ts[679-706]
### Proposed fix
Update the table to reflect the actual normalized types, e.g.:
- PostgreSQL: `string` -> `character varying`, `number` -> `integer`, `Date` -> `timestamp without time zone`
- SQLite: `string` -> `varchar`
…and adjust wording to clarify these are `normalizeType` outputs (and may be equivalent/aliased in some DBs).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


4. Inference prerequisites missing ☑ 🐞 ≡
Description
The new section says TypeORM automatically infers types when @Column() omits type, but inference
depends on Reflect metadata; without reflect-metadata and emitDecoratorMetadata, TypeORM throws
ColumnTypeUndefinedError.
Code

docs/docs/entity/1-entities.md[R217-218]

+When no explicit type is provided in the `@Column()` decorator, TypeORM automatically infers the database column type based on the TypeScript property type.
+
Evidence
@Column() only guesses a type when Reflect.getMetadata('design:type', ...) is available;
otherwise, if no explicit options.type is set, it throws ColumnTypeUndefinedError, whose message
explicitly calls out emitDecoratorMetadata and importing reflect-metadata.

docs/docs/entity/1-entities.md[215-224]
src/decorator/columns/Column.ts[190-228]
src/error/ColumnTypeUndefinedError.ts[1-15]

Agent prompt
The issue below was found during a code review. Follow the provided context and guidance below and implement a solution

## Issue description
The new docs imply inference always works when `@Column()` omits `type`, but inference fails (and TypeORM throws) if Reflect metadata isn’t available.
### Issue Context
`@Column()` uses `Reflect.getMetadata('design:type')` to infer the type. When that metadata is missing, `ColumnTypeUndefinedError` is thrown, with guidance about `emitDecoratorMetadata` and importing `reflect-metadata`.
### Fix Focus Areas
- docs/docs/entity/1-entities.md[215-224]
- src/decorator/columns/Column.ts[190-228]
- src/error/ColumnTypeUndefinedError.ts[1-15]
### Proposed fix
Add a short note under “Column Type Inference” stating that inference requires `reflect-metadata` to be imported and `emitDecoratorMetadata: true` (TypeScript), and that otherwise you must specify `type` explicitly (or JS users must always specify it).

ⓘ Copy this prompt and use it to remediate the issue with your preferred AI generation tools


Grey Divider

Grey Divider

Qodo Logo

@ElwinErnst

Copy link
Copy Markdown
Author

All review comments addressed in the latest update ✅

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 2ff1d03

@ElwinErnst

Copy link
Copy Markdown
Author

Thanks for the feedback.

I intentionally avoided documenting specific defaults (such as varchar length or exact string → varchar/text mapping) because this behavior is driver-specific and not consistently defined across all databases supported by TypeORM.

Instead, I clarified that:

  • inference depends on the driver implementation
  • mappings are only common cases (not guaranteed)
  • explicit column types should be used in production

Documenting fixed defaults here could be misleading for users, especially across different drivers or configurations.

If there is a specific source of truth in the codebase that defines consistent defaults per driver, I’d be happy to extend this section with more precise details.

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit 2e0745c

@sonarqubecloud

Copy link
Copy Markdown

@qodo-free-for-open-source-projects

Copy link
Copy Markdown

Persistent review updated to latest commit bb1c65c

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Development

Successfully merging this pull request may close these issues.

No docs on inferred column types

2 participants