Hiral Gandhi
Hiral Gandhi

Reputation: 15

Mysql syntaxerror while trying multiple image upload in rails using carrierwave gem

I'm following carrierwave documentation here to upload multiple images to my listing model. When, I used the command

rails g migration add_images_to_listings images:json

migration gets successfully created like this -

class AddImagesToListings < ActiveRecord::Migration
  def change
    add_column :listings, :images, :json
  end
end

but running rake db:migrate throws a mysql syntax error

     Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to
your MySQL server version for the right syntax to use near 'json'
     at line 1: ALTER TABLE `listings` ADD `images` json/usr/local/rvm/gems/ruby-2.2.1/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract_mysql_adapter.rb:299:in `query'

I suspect this is because json data type is not supported with mysql. Is there some workaround?

Upvotes: 1

Views: 299

Answers (1)

pedrocalgaro
pedrocalgaro

Reputation: 43

As you can see here https://dev.mysql.com/doc/refman/5.7/en/json.html

JSON datatype is available only on Mysql 5.7.8 or above.

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

MySQL JSON type was added on Rails 5.

So if your setup is below these configurations, you cant use JSON with Mysql.

But you can workaround this problem using a :text type on your migration:

class AddImagesToListings < ActiveRecord::Migration
  def change
    add_column :listings, :images, :text
  end
end

Upvotes: 0

Related Questions