Skip to content

Nullable columns in typescript interface declaration #553

Description

@information-security

Problem
I can not set a nullable column to null using .update function after upgrading sequelize to 6.6.2.
Executing user.update( { email: null });, results in following error:

TS2322: Type 'null' is not assignable to type 'string | Literal | Fn | Col | undefined'. 

user.ts(11, 3): The expected type comes from property 'email' which is declared here on type
{
  id?: number | Literal | Fn | Col | undefined;
  email?: string | Literal | Fn | Col | undefined;
  password?: string | Literal | Fn | Col | undefined;
  ... 14 more ...;
  last_login_date?: Literal | ... 3 more ... | undefined;
}

Hence, I believe null type must be added into attributes declaration.

Example
Consider following schema as an example:

CREATE TABLE user {
  `email` varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL
}

Output
The generated interface would be like below:

export interface userAttributes {
 
  email?: string;
}

export class user extends Model<userAttributes, userCreationAttributes> implements userAttributes {

    email?: string;

    static initModel(sequelize: Sequelize.Sequelize): typeof user {

        user.init({
            email: {
  type: DataTypes.STRING(255),
  allowNull: true 
},
        });
        return user;
    }
}

Expected output

export interface userAttributes {
 
  email?: string|null;
}

export class user extends Model<userAttributes, userCreationAttributes> implements userAttributes {

    email?: string|null;

    static initModel(sequelize: Sequelize.Sequelize): typeof user {

        user.init({
            email: {
  type: DataTypes.STRING(255),
  allowNull: true 
},
        });
        return user;
    }
}

Disclaimer
I might be misunderstanding something here. So if there are any solutions to this problem please let me know. Additionally, PR#547 might be a workaround to this problem.

Environment
sequelize version: 6.6.2
sequelize-auto version: 0.8.4
dialect: mysql
node version: 16.6.1
npm version: 7.21.1
CMD: sequelize-auto -o "./src/models" -l ts -e mysql

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions