【问题标题】:Waterline BIGINT type with sails-mysql带有sails-mysql的Waterline BIGINT类型
【发布时间】:2017-04-07 21:27:27
【问题描述】:

我想知道如何使用sails-mysql 在水线模型中定义bigint 类型?找不到任何关于它的适当文档。似乎它不支持 bigint 类型,但我真的需要它。试图挖掘源代码我发现了一些类似的东西: https://github.com/balderdashy/sails-mysql/blob/987f4674785970951bc52becdfdb479864106da1/helpers/private/schema/build-schema.js#L29 但是还是不行。

module.exports = {
    attributes: {

        userId: {
            type: 'bigint',
            autoIncrement: true,
            primaryKey: true,
            unique: true,
        },
   }
};

这个仍然在数据库中创建一个整数字段。

【问题讨论】:

    标签: mysql sails.js waterline


    【解决方案1】:

    好吧,在深入研究了源代码之后,我发现我必须为该字段设置一个名为 size 的额外属性。将其设置为 64 将导致水线创建一个 BIGINT 字段。

    module.exports = {
        attributes: {
    
            userId: {
                type: 'integer',
                size: 64, // waterline will translate this as bigint
                autoIncrement: true,
                primaryKey: true,
                unique: true,
            },
       }
    };
    

    【讨论】:

    • 不幸的是,这似乎不适用于 Sails v1.0+。我收到此错误:myModel 模型上的属性 myAttribute 包含无效属性。 size 的属性不是公认的属性。
    【解决方案2】:

    另一种方法是使用以下属性:

    {
       type: 'string',
       columnType: 'bigint'
    }
    

    这会忽略 Waterline 数据类型并直接强制 Postgres/MySQL 列数据类型。

    【讨论】:

    • 我们为什么不用javascript的数字类型呢?
    • 问题是最大“bigint”值大于最大JavaScript整数值,因此需要将其存储为字符串。更多信息在这里:github.com/balderdashy/waterline/issues/1487这看起来是 Waterline/Sails 的一个开发领域,因此可能会在未来的版本中得到修复:trello.com/c/VgpoYb9j/…
    • Node.Js Number: Number.MAX_SAFE_INTEGER: 9007199254740991 Mysql BigInt Type: Max Value: 9223372036854775807 我发现没有人会存储这么多数据,除非你正在做一些非常大的事情。但确实是水线应该处理的问题。但是,根据具体情况,我建议切换到不同的 ODM,因为水线确实不适合需要处理这么多数据的大规模应用程序。
    【解决方案3】:

    编辑:我没有彻底测试过。毕竟它不起作用。

    Sails v1.0+ 中的模型属性不允许使用 size 属性,但这似乎可行。我从 the waterline library 中的 createdAt 属性的定义中提取了它(在撰写本文时,第 485 行)。

    module.exports = {
      attributes: {
        timeOfDay: {
          type: 'number',
          autoMigrations: { columnType: '_numbertimestamp' }
        }
      }
    };
    

    【讨论】:

    • 顺便说一下,这不应该是特定于 mySQL 的。我正在使用 postgreSQL。
    猜你喜欢
    • 2020-07-20
    • 2019-08-26
    • 2017-01-01
    • 2018-07-29
    • 2015-11-21
    • 2014-07-18
    • 1970-01-01
    • 2014-05-12
    • 2015-06-21
    相关资源
    最近更新 更多