Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use json column type in Rails MySQL app?

Because of server requirements, I'm using MySQL 5.5 in my Rails app.

I would like to use json column type, similar to PostgreSQL, but when I run migration I'm getting error, that json is not supported.

My migration:

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

Can somebody suggest the best solution how to fix this issue?

like image 990
Derk153 Avatar asked Sep 13 '25 20:09

Derk153


1 Answers

Since MySQL 5.5 doesn't have JSON data type, in a Rails 4 app, you should use the more database agnostic TEXT type that will work in both MySQL and PostgreSQL.

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

As of MySQL 5.7.8, the JSON column type has been added.

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

As of Rails 5, MySQL JSON type has been added.

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

like image 121
SacWebDeveloper Avatar answered Sep 16 '25 11:09

SacWebDeveloper