Rails 3: migration error when using json as a column type in an ActiveRecord backed by Postgres

前端 未结 3 1946
别跟我提以往
别跟我提以往 2020-12-28 15:39

I am running Rails 3.2.17 and Postgres 9.3.4. I created a new ActiveRecord model using \"rails generate\" and one of the column types is json. My intention is to use the jso

相关标签:
3条回答
  • 2020-12-28 16:15

    Change your migration like

    class CreateThing < ActiveRecord::Migration
      def change
        create_table :things do |t|
          t.integer :user_id
          t.column :json_data, :json   # Edited
          t.timestamps
        end
        add_index :things, :user_id
      end
    end
    

    And by default rake db tasks will look into schema.rb( which wont be the case for postgres) so in application.rb change it to

    config.active_record.schema_format = :sql
    
    0 讨论(0)
  • 2020-12-28 16:16

    Set the following in application.rb

    config.active_record.schema_format = :sql
    

    Then structure.sql will be used instead of schema.rb to create the database from scratch. More info - https://github.com/diogob/activerecord-postgres-hstore

    0 讨论(0)
  • 2020-12-28 16:19

    We have a legacy application that uses activerecord-postgres-json to add json and jsonb type support to ActiveRecord in Rails 3.2; it may be an option if you're stuck at a low Rails version.

    0 讨论(0)
提交回复
热议问题