1

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?

1
  • As I see here, you can use JSON datatype with PostgreSQL Commented Sep 30, 2015 at 14:02

1 Answer 1

0

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
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, but I am trying to understand how to get from that text field the different url of every image.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.