【问题标题】:Sequelize altering all table fields to snake case续集将所有表字段更改为蛇案例
【发布时间】:2019-04-05 20:58:34
【问题描述】:

我正在尝试进行迁移以将所有现有的表字段更改为蛇案例,并且将来的表字段已经以这种方式编写。 到目前为止,我一直在使用

myTableField = {type: datatype.stmh, field: 'my_table_field'}

事情是我想停止这样做,对于即将到来的领域,我只需要直接用蛇盒设置它们。

有没有办法做到这一点?或者我必须为每个手动运行一个更改字段?

【问题讨论】:

    标签: node.js sequelize.js


    【解决方案1】:

    通过下划线模型选项,可以不在模型定义中将每个字段重命名为下划线。

    options.underscored - Converts all camelCased columns to underscored if true
    options.underscoredAll - Converts camelCased model names to underscored table names if true
    

    例如:

    const User = sequelize.define('User', {
                   firstname: Sequelize.STRING,
                   lastname: Sequelize.STRING
                 },{ 
                    underscored:true})
    

    同样在 CLI 上,我们可以选择使用蛇形盒生成迁移

    >> sequelize model:generate --name User --attributes firstName:string,lastName:string  --underscored 
    

    在哪里

     --underscored      Use snake case for the timestamp's attribute names    
    

    但它仅适用于迁移定义中的时间戳字段。

    【讨论】:

      猜你喜欢
      • 2022-10-21
      • 1970-01-01
      • 1970-01-01
      • 2020-05-03
      • 2019-10-30
      • 2020-02-10
      • 2021-03-31
      • 1970-01-01
      • 2013-11-20
      相关资源
      最近更新 更多