2015-09-30 62 views
1

我正在关注载波文档here以将多个图像上传到我的列表模型。 时,我用在使用载波宝石在轨中尝试多个图像上载时的Mysql语法错误

rails g migration add_images_to_listings images:json 

迁移被成功地这样创建的命令 -

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

但运行耙分贝:迁移抛出一个MySQL的语法错误

 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' 

我怀疑这是因为json数据类型不被mysql支持。有一些解决方法吗?

+0

正如我见[这里](http://stackoverflow.com/a/17918118/2968762),你可以使用JSON数据类型与PostgreSQL – Abhi

回答

0

正如你可以在这里看到https://dev.mysql.com/doc/refman/5.7/en/json.html

JSON数据类型仅适用于5.7.8的Mysql或以上。

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

MySQL的JSON类型是on Rails的5

所以,如果你设置如下这些配置,您不能使用JSON与MySQL增加。

但是你可以使用一个解决此问题:在您的迁移文本类型:

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

谢谢,但我想了解如何从该文本字段获取每个图像的不同url。 –

相关问题