Hei*_*ing 22 postgresql ruby-on-rails
我正在运行Rails 3.2.17和Postgres 9.3.4.我使用"rails generate"创建了一个新的ActiveRecord模型,其中一个列类型是json.我的目的是在Postgres中使用json列类型.
db迁移包含以下代码:
class CreateThing < ActiveRecord::Migration
def change
create_table :things do |t|
t.integer :user_id
t.json :json_data
t.timestamps
end
add_index :things, :user_id
end
end
Run Code Online (Sandbox Code Playgroud)
当我尝试使用"rake db:migrate"进行迁移时出现此错误:
-- create_table(:things)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
undefined method `json' for #<ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::TableDefinition:0x007fea3d465af8>/Users/../db/migrate/20140425030855_create_things.rb:7:in `block in change'
/Library/Ruby/Gems/2.0.0/gems/activerecord-3.2.17/lib/active_record/connection_adapters/abstract/schema_statements.rb:160:in `create_table'
Run Code Online (Sandbox Code Playgroud)
这是将json列添加到ActiveRecord的正确方法吗?我找不到任何文档或示例.谢谢!
Siv*_*iva 32
改变您的迁移
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
Run Code Online (Sandbox Code Playgroud)
默认情况下,rake db任务将查看schema.rb(对于postgres不会是这种情况)所以在application.rb中将其更改为
config.active_record.schema_format = :sql
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7737 次 |
| 最近记录: |