Skip to content

Add nvarchar and datetime2 native type support in SQL Server type converter #1137

@arodriguezf

Description

@arodriguezf

Description
The convert_type_to_sqlserver function in sql_type_converter.py currently does not handle nvarchar and datetime2 as explicit base types. When a data contract field uses these SQL Server-specific types directly, they are not properly mapped and fall through the conversion logic.

Proposed Change
Add explicit handling for nvarchar and datetime2 in convert_type_to_sqlserver:

  • nvarchar: When a field's base type is nvarchar, it should be passed through _attach_params_if_present("nvarchar", field) to preserve any length parameters (e.g., nvarchar(255)).

  • datetime2: When a field's base type is datetime2, it should be passed through _attach_params_if_present("datetime2", field) to preserve any precision parameters.

Code Change

# In datacontract/export/sql_type_converter.py — convert_type_to_sqlserver()

if base_type in ["nvarchar"]:
    return _attach_params_if_present("nvarchar", field)

if base_type in ["datetime2"]:
    return _attach_params_if_present("datetime2", field)

Motivation
Users working with SQL Server data contracts may define fields with native SQL Server types like nvarchar (for Unicode strings) or datetime2 (for high-precision timestamps). Without this handling, exporting such contracts to SQL doesn't produce the correct DDL.

Note
I wanted to send you the PR for your review, but I don´t have permissions to push branches.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions