Skip to content

SQL Server CREATE TABLE not setting columns NULLABLE by default #1680

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
kenny-statsig opened this issue Mar 14, 2025 · 2 comments
Open

SQL Server CREATE TABLE not setting columns NULLABLE by default #1680

kenny-statsig opened this issue Mar 14, 2025 · 2 comments

Comments

@kenny-statsig
Copy link

kenny-statsig commented Mar 14, 2025

Software versions

  • Tedious: 19
  • SQL Server: default (no external library)
  • Node.js: 23.6.1

Additional Libraries Used and Versions

Table schema

Connection configuration

      server: data.connectionString,
      authentication: {
        type: 'azure-active-directory-service-principal-secret',
        options: {
          clientId: data.clientId,
          clientSecret: data.clientSecret,
          tenantId: data.tenantId,
        },
      }
      options: {
        encrypt: true,
        database: data.database,
        port: 1433,
        rowCollectionOnRequestCompletion: true,
        connectTimeout: 30000,
        requestTimeout: 30000,
      },

Problem description

Executing the following query using tedious creates a table but the resulting table's columns are all NON NULL columns. Executing it directly from Fabric Warehouse successfully defines the columns as NULL-able

IF NOT EXISTS (
    SELECT * FROM
        INFORMATION_SCHEMA.TABLES
    WHERE
        TABLE_SCHEMA = 'dbo'
        AND
            TABLE_NAME = 'pipeline_overview'
)
CREATE TABLE [dbo].[pipeline_overview] (
    ts DATETIME2 (6),
    dag_id VARCHAR (500),
    job_type VARCHAR (500),
    metric_source_id VARCHAR (500),
    assignment_source_id VARCHAR (500),
    job_status VARCHAR (500),
    metrics VARCHAR (MAX),
    dag_state VARCHAR (500),
    dag_type VARCHAR (500),
    experiment_id VARCHAR (500),
    dag_start_ds VARCHAR (500),
    dag_end_ds VARCHAR (500),
    wall_time NUMERIC,
    turbo_mode BIT,
    dag_duration NUMERIC,
    is_scheduled BIT,
    experiment_team VARCHAR (500),
    metric_source_team VARCHAR (500),
    creator VARCHAR (500)
);

Expected behavior

The columns should be NULL-able.
Executing the following query should work

INSERT INTO [dbo].[pipeline_overview] (
    ts,
    job_type,
    metric_source_id,
    assignment_source_id,
    job_status,
    metrics,
    dag_state,
    dag_type,
    experiment_id,
    dag_start_ds,
    dag_end_ds,
    wall_time,
    turbo_mode,
    dag_id,
    dag_duration,
    is_scheduled,
    experiment_team,
    metric_source_team,
    creator
)
VALUES (
    '2025-03-14T18:55:13.557Z',
    'assignment_source_sync',
    NULL,
    NULL,
    'success',
    JSON_ARRAY (
    ),
'success',
'assignment_source',
'6aX4dteZLrnzNd5zU0wy0T',
'2025-03-14',
'2025-03-14',
5892.51879099998,
0,
'6Idu2zWvr7xScQQxr9Z4zx',
0,
0,
NULL,
NULL,
'Kenny Yi'
),

    (
        '2025-03-14T18:55:13.557Z',
        'assignment_source_sync',
        NULL,
        NULL,
        'success',
JSON_ARRAY 
    (
    ),
    'success',
    'assignment_source',
    '6aX4dteZLrnzNd5zU0wy0T',
    '2025-03-14',
    '2025-03-14',
    5872.721833999996,
    0,
    '6Idu2zWvr7xScQQxr9Z4zx',
    0,
    0,
    NULL,
    NULL,
    'Kenny Yi'
    )

Actual behavior

The columns are NOT NULL-able.
Executing the query gives the error below

Error message/stack trace

 Cannot insert the value NULL into column 'metric_source_id', table 'pipeline_overview'; column does not allow nulls. INSERT fails

Any other details that can be helpful

I am on a forked version of the library https://github.com/statsig-io/tedious. Based on the uncommitted PR #1668

@khkiley
Copy link

khkiley commented Mar 17, 2025

Interesting, what happens when you specify the nullability of the column, i.e. :

CREATE TABLE [dbo].[pipeline_overview] (
    ts DATETIME2 (6) NOT NULL,
    dag_id VARCHAR (500) NOT NULL,
    job_type VARCHAR (500) NULL,
    metric_source_id VARCHAR (500) NULL ,
    ...
)

Have you tried executing the CREATE TABLE statement directly against the database ?

@kenny-statsig
Copy link
Author

Executing the same CREATE TABLE query directly against the database makes the columns NULLABLE.
I have worked around this by manually specifying the nullability. But I would expect this not to be necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants