无法将数组存储在Postgresql中的JSON字段中(Rails)无法将数组转换为JSON [英] Can't store array in json field in postgresql (rails) can't cast Array to json

查看:123
本文介绍了无法将数组存储在Postgresql中的JSON字段中(Rails)无法将数组转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我在运行db:migrate

This is the error I'm getting when I run db:migrate

rake aborted!
can't cast Array to json

这是我的表

  class CreateTrips < ActiveRecord::Migration

          def change
            create_table :trips do |t|

              t.json :flights
              t.timestamps
            end
          end 
        end

这是在我的seed.rb文件中

This is in my seeds.rb file

flights = [{
    depart_time_hour: 600,
    arrive_time_hour: 700,

    passengers: [
        {
            user_id: 1,
            request: true    
        }
    ]
}]

trip = Trip.create(
  {
    name: 'Flight',

    flights: flights.to_json 
  }
)

出于某种原因我可以做这个。

For some reason I can't do this. If I do this.

trip = Trip.create(
      {
        name: 'Flight',
        flights: { flights: flights.to_json }
      }
    )

有效。我不想要这个,因为现在我必须使用trip.flights.flights访问json数组。不是我想要的行为。

It works. I don't want this though because now I have to access the json array with trip.flights.flights. Not the behavior I'm wanting.

推荐答案

执行摘要:这是已知的问题,并在此拉动请求中解决,在撰写本文时,正在等待合并。

Exec Summary: This is a known issue and is addressed in this pull request, which is awaiting merge as of this writing.

长版:

嗯,我明白了为什么和它如何基于阵列/哈希失败/成功。这是进行类型转换的Rails方法(来自quoting.rb),它显然不支持从Ruby Array 转换为Postgres json ,但确实支持从 Hash 进行投射。另外,我在此方法的开头放了一些调试代码,发现使用 flights flights.to_json 作为值,因为此转换将后者转换为前者。我将做更多的挖掘工作,因为使用psql将 flights.to_json 的值插入json列中没有问题。

Well, I can see why and how it's failing/succeeding based on Array/Hash. Here's the Rails method that's doing the type conversion (from quoting.rb) and it clearly doesn't support casting from Ruby Array to Postgres json while it does support casting from Hash. Also, I put some debugging code at the beginning of this method and found that it doesn't matter if you use flights or flights.to_json as the value, as the latter gets converted to the former for purposes of this casting. I'm going to do some more digging because I had no problem inserting the value of flights.to_json into a json column using psql.

    def type_cast(value, column, array_member = false)
      return super(value, column) unless column

      case value
      when Range
        return super(value, column) unless /range$/ =~ column.sql_type
        PostgreSQLColumn.range_to_string(value)
      when NilClass
        if column.array && array_member
          'NULL'
        elsif column.array
          value
        else
          super(value, column)
        end
      when Array
        case column.sql_type
        when 'point' then PostgreSQLColumn.point_to_string(value)
        else
          return super(value, column) unless column.array
          PostgreSQLColumn.array_to_string(value, column, self)
        end
      when String
        return super(value, column) unless 'bytea' == column.sql_type
        { :value => value, :format => 1 }
      when Hash
        case column.sql_type
        when 'hstore' then PostgreSQLColumn.hstore_to_string(value)
        when 'json' then PostgreSQLColumn.json_to_string(value)
        else super(value, column)
        end
      when IPAddr
        return super(value, column) unless ['inet','cidr'].include? column.sql_type
        PostgreSQLColumn.cidr_to_string(value)
      else
        super(value, column)
      end
    end

我继续往 Array 例中添加了以下行:

I went ahead and added the following line to the Array case:

        when 'json' then PostgreSQLColumn.json_to_string(value)

,然后将 PostgreSQLColumn.json_to_string (在cast.rb中)更改为对 Array 以及 Hash 类型,我就能通过您的用例。

and then changed PostgreSQLColumn.json_to_string (in cast.rb) to operate on arguments of Array as well as Hash type and I was able to get your use case to pass.

我还没有检查是否在这一点上有任何问题或请求

I haven't checked to see if there are any issues or pull requests on this at this point

BTW,我假设您知道您可以使用 text 字段,而不是 json 字段。 json 为您提供的唯一信息是数据库级别的验证。如果您认为这很重要,那么我想知道为什么,因为我正在研究的Web应用程序中有一些带有json内容的文本字段,并且我想知道转换它们的好处(如果有)

BTW, I assume you know you can work around this by using a text field instead of a json field. The only thing that json provides you that I know if is validation at the database level. If you think that's important, I'd be interested in knowing why, as I've got some text fields with json content in the web app I'm working on and I'd like to know the benefit of converting them, if any.

这篇关于无法将数组存储在Postgresql中的JSON字段中(Rails)无法将数组转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆