【问题标题】:How can I use json column type in Rails MySQL app?如何在 Rails MySQL 应用程序中使用 json 列类型?
【发布时间】:2016-03-06 05:08:50
【问题描述】:

由于服务器要求,我在我的 Rails 应用程序中使用 MySQL 5.5。

我想使用 json 列类型,类似于 PostgreSQL,但是当我运行迁移时出现错误,不支持该 json。

我的迁移:

def change
    add_column :profile_services, :price, :json
end

有人可以建议如何解决此问题的最佳解决方案吗?

【问题讨论】:

    标签: mysql json ruby-on-rails-4


    【解决方案1】:

    由于 MySQL 5.5 没有 JSON 数据类型,因此在 Rails 4 应用程序中,您应该使用在 MySQL 和 PostgreSQL 中都可以使用的更多与数据库无关的 TEXT 类型。

    def change
        add_column :profile_services, :price, :text
    end
    

    从 MySQL 5.7.8 开始,添加了 JSON 列类型。

    https://dev.mysql.com/doc/refman/5.7/en/json.html

    从 Rails 5 开始,添加了 MySQL JSON 类型。

    https://github.com/rails/rails/pull/21110

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-02-27
      • 1970-01-01
      相关资源
      最近更新 更多