【问题标题】:How to auto generate a rake migration file with json如何使用 json 自动生成 rake 迁移文件
【发布时间】:2015-01-20 15:26:23
【问题描述】:

使用 sqlite3,Rails 4.2.0。 运行rake db:migrate 时出现以下错误。 问题是迁移文件有以下内容:

t.json :blah 应该有t.column :blah, :json

我的问题是,“如何让迁移文件正确自动生成?

我用它来生成迁移文件:

rails generate scaffold rails_app title:string blah:json

这会将t.json :blah 放入迁移文件中。

> ubuntu@host:~/rails_app$ rake db:migrate
> == 20150119183550 CreatePolreqs: migrating ====================================
> -- create_table(:rails_app) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:
> 
> undefined method `json' for
> #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007f8466ab9680>/home/ubuntu/rails_app/db/migrate/20150119183550_create_rails_app.rb:6:in
> `block in change'
> /home/ubuntu/rails-app/db/migrate/20150119183550_create_rails_app.rb:3:in
> `change' NoMethodError: undefined method `json' for
> #<ActiveRecord::ConnectionAdapters::TableDefinition:0x007f8466ab9680> /home/ubuntu/rails_app/db/migrate/20150119183550_create_rails_app.rb:6:in
> `block in change'
> /home/ubuntu/rails_app/db/migrate/20150119183550_create_rails_app.rb:3:in
> `change' Tasks: TOP => db:migrate (See full trace by running task with
> --trace)

【问题讨论】:

  • 我遇到了同样的问题。似乎迁移文件总是将 t.json :blah 写回模式文件;但是架构文件需要 t.column :blah, :json 的格式。也许它需要 Rails 团队重写模式文件的更新方式。有什么解决方案/解决方法吗?

标签: json migration rake


【解决方案1】:

sqlite3 适配器不支持 json 列(目前)。

postgres 支持:http://guides.rubyonrails.org/v4.2/active_record_postgresql.html

要切换数据库,请更新您的 Gemfile 并替换

gem 'sqlite3' 

gem 'pg'

切换数据库的替代方法是使用“文本”列而不是“json”,但这需要自己序列化 json,并且您不会得到 postgres 提供的任何花哨的 json 查询运算符。

看起来 sqlite3 最近添加了对 json 列的支持,因此 sqlite-ruby 适配器可能会在未来某个时候支持:

http://www.infoq.com/news/2015/10/sqlite-39-released

https://github.com/sparklemotion/sqlite3-ruby

【讨论】:

    猜你喜欢
    • 2020-11-05
    • 2012-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2021-03-17
    • 2015-03-06
    • 1970-01-01
    相关资源
    最近更新 更多