how to set mysql datetype length with sequelize-cli

允我心安 提交于 2020-08-06 06:10:47

问题


sequelize/CLI version: "sequelize-cli": "^6.2.0","sequelize": "^6.3.3"

i'm using this to generate a mysql user table

npx sequelize-cli model:generate --name User --attributes firstName:string,lastName:string,email:string

expect to generate an atrribute with length with sequelize-cli

firstName:DataTypes.STRING(20) // model with length

npx sequelize-cli model:generate --name User --attributes firstName:string // how to add length with cli?

didnt find anything through the documentation and the source code, is this no need? please, anyone knows what's going on here?


回答1:


Currently there is no option to generate model with detailed attributes. You can check the responsible code here. It is pretty clear code. Easy to understand.

I usually just generate it with name and no fields and then copy paste my model to generated file.

This is the model.

class MyModel extends Sequelize.Model { }
MyModel.init({
    name: {
        type: Sequelize.DataTypes.STRING(100),
        allowNull: false,
        validate: {
            notNull: true,
            notEmpty: true,
            len: [2, 100]
        }
    },
    description: {
        type: Sequelize.DataTypes.STRING(5000),
        allowNull: false,
        validate: {
            notNull: true,
            notEmpty: true,
            len: [100, 5000]
        }
    }
}, { sequelize: sequelizeInstance });

I run sequelize-cli model:generate --name MyModel and copy paste all the init parameter object directly inside the generated file. Like this:

queryInterface.createTable(
    'MyModel',
    {
        name: {
            type: Sequelize.DataTypes.STRING(100),
            allowNull: false,
            validate: {
                notNull: true,
                notEmpty: true,
                len: [2, 100]
            }
        },
        description: {
            type: Sequelize.DataTypes.STRING(5000),
            allowNull: false,
            validate: {
                notNull: true,
                notEmpty: true,
                len: [100, 5000]
            }
        }
    }
);

Of course we don't need validations here and also we need some extra fields like Id and foreign keys if there is one to many association. Don't forget to add updatedAt and createdAt if you allow sequelize to add it to your model instance.

So remove validate and add the others.

queryInterface.createTable(
    'MyModel',
    {
        id: {
            type: Sequelize.DataTypes.INTEGER,
            primaryKey: true,
            autoIncrement: true
        },
        name: {
            type: Sequelize.DataTypes.STRING(100),
            allowNull: false
        },
        description: {
            type: Sequelize.DataTypes.STRING(5000),
            allowNull: false
        },
        createdAt: {
            type: Sequelize.DataTypes.DATE,
            allowNull: false,
        },
        updatedAt: {
            type: Sequelize.DataTypes.DATE,
            allowNull: false,
        },
        MyOtherModelId: {
            type: Sequelize.DataTypes.INTEGER,
            allowNull: false,
            references: {
                model: 'MyOtherModel'
            },
            onUpdate: 'cascade',
            onDelete: 'restrict'
        }
    }
);

That's how I manage to create my migrations from my models. Unfortunately sequelize cli does not have any detailed options for generate command. But feel free to add some! Pull it from github and work on it. Would be nice to have. You can also automate this described process and add it as another command to sequelize cli.



来源:https://stackoverflow.com/questions/63125741/how-to-set-mysql-datetype-length-with-sequelize-cli

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!