Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use PostgreSQL JSON column type with Rails 3

The following migration in Rails 3 works:

class CreateUserActions < ActiveRecord::Migration
  def up
    create_table :user_actions do |t|
      t.datetime :time
      t.integer  :user_id
      t.text     :action
      t.column   :details, :json
      t.timestamps
    end
  end

  def down
    drop_table 'user_actions'
  end
end

...but schema.rb is now incomplete reporting

# Could not dump table "user_actions" because of following StandardError
#   Unknown type 'json' for column 'details'

So rake db:reset will fail to create the user_actions table.

like image 703
crizCraig Avatar asked Dec 19 '13 03:12

crizCraig


1 Answers

From: https://github.com/diogob/activerecord-postgres-hstore just set the following in application.rb:

config.active_record.schema_format = :sql

Now structure.sql will be used instead of schema.rb to create the database from scratch with rake db:reset or rake db:prepare and will be specific to PostGres.

like image 67
crizCraig Avatar answered Sep 19 '22 08:09

crizCraig