如何在Rails MySQL应用程序中使用json列类型?

Der*_*153 3 mysql json ruby-on-rails-4

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

我想使用json列类型,类似于PostgreSQL,但是当我运行迁移时,我收到错误,不支持json.

我的迁移:

def change
    add_column :profile_services, :price, :json
end
Run Code Online (Sandbox Code Playgroud)

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

Sac*_*per 6

由于MySQL 5.5没有JSON数据类型,因此在Rails 4应用程序中,您应该使用更多数据库不可知的TEXT类型,它将在MySQL和PostgreSQL中运行.

def change
    add_column :profile_services, :price, :text
end
Run Code Online (Sandbox Code Playgroud)

从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