如何使用sequelize-cli设置mysql datetype长度 [英] how to set mysql datetype length with sequelize-cli

查看:280
本文介绍了如何使用sequelize-cli设置mysql datetype长度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

sequelize/CLI版本:"sequelize-cli":"^ 6.2.0","sequelize":"^ 6.3.3"

我正在使用它来生成mysql用户表

i'm using this to generate a mysql user table

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

期望使用sequelize-cli生成长度不全的特技

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?

推荐答案

当前没有选择生成具有详细属性的模型的选项. 您可以在此处查看负责的代码.这是非常清晰的代码.容易理解.

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.

这是模型.

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 });

我运行sequelize-cli model:generate --name MyModel并将所有init参数对象直接复制粘贴到生成的文件中.像这样:

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]
            }
        }
    }
);

当然,这里我们不需要验证,并且如果存在一对多关联,我们还需要一些额外的字段,例如Id和外键.如果允许sequelize将其添加到模型实例中,请不要忘记添加updatedAt和createdAt.

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'
        }
    }
);

这就是我如何从模型创建迁移的方法.不幸的是,sequelize cli没有任何用于生成命令的详细选项.但是随时添加一些!从github上提取它并进行处理.会很高兴.您还可以使描述的过程自动化,并将其添加为另一个命令以对cli进行排序.

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.

这篇关于如何使用sequelize-cli设置mysql datetype长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆