【问题标题】:Mysql syntaxerror while trying multiple image upload in rails using carrierwave gem尝试使用carrierwave gem在rails中上传多个图像时出现Mysql语法错误
【发布时间】:2015-09-30 12:37:09
【问题描述】:

我正在关注carrierwave 文档here 将多张图片上传到我的列表模型。 什么时候,我用了命令

rails g migration add_images_to_listings images:json

像这样成功创建迁移 -

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

但运行 rake db:migrate 会引发 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'

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

【问题讨论】:

  • 如我所见here,您可以在 PostgreSQL 中使用 JSON 数据类型

标签: mysql ruby-on-rails ruby json carrierwave


【解决方案1】:

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

JSON 数据类型仅适用于 Mysql 5.7.8 或更高版本。

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

Rails 5 中添加了 MySQL JSON 类型。

因此,如果您的设置低于这些配置,则无法将 JSON 与 Mysql 一起使用。

但您可以在迁移时使用 :text 类型解决此问题:

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

【讨论】:

  • 谢谢,但我想了解如何从该文本字段中获取每张图片的不同网址。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-06-04
  • 1970-01-01
  • 1970-01-01
  • 2012-08-17
  • 2021-05-13
相关资源
最近更新 更多