如何使用 sequelize-cli 设置 mysql 日期类型长度

how to set mysql datetype length with sequelize-cli(如何使用 sequelize-cli 设置 mysql 日期类型长度)

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

问题描述

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.

所以删除 validate 并添加其他的.

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 日期类型长度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用 sequelize-cli 设置 mysql 日期类型长度

基础教程推荐