database - #由于遵循NoMethodError而无法转储表“预订”#nil:NilClass的未定义方法'[]'

标签 database ruby-on-rails-4 sqlite migration schema

我开始了一个新项目:

我只运行以下命令:

rails g model subscription


然后:

rails g controller subscriptions


然后:

已将resources :subscriptions添加到我的路线

然后:

rake db:migrate


然后:

rails g migration add_column_to_subscriptions


具有以下内容:

class AddColumnsToSubscriptions < ActiveRecord::Migration

      def change
        add_column :subscriptions, :fname, :text
        add_column :subscriptions, :lname, :text
        add_column :subscriptions, :email, :email
      end
    end


然后:

rake db:migrate


在执行添加列迁移之前,我的架构中有一个订阅表。但现在在模式中,我有:

# Could not dump table "subscriptions" because of following NoMethodError
#   undefined method `[]' for nil:NilClass


这里发生了什么?我发现的SO解决方案都没有对我有用,或者我不理解。

最佳答案

不知道此错误是否仍在为您发生,但是否为其他人发生。问题是您的数据库可能处于时髦状态,列类型错误,我猜是这种情况下的“电子邮件”。尝试将其更改为字符串,然后重新开始。

class AddColumnsToSubscriptions < ActiveRecord::Migration
  def change
    add_column :subscriptions, :fname, :string
    add_column :subscriptions, :lname, :string
    add_column :subscriptions, :email, :string
  end
end


另外,我建议使用字符串作为名字和姓氏,因为它们可能永远不会超过256个字符。这将使您的数据库更有效率。它对我有用,所以也许可以帮助到那里的其他人。

关于database - #由于遵循NoMethodError而无法转储表“预订”#nil:NilClass的未定义方法'[]',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23909018/

相关文章:

java - 在Spring Data JPA中获取列的所有记录,即使我们将其指定为参数

ruby-on-rails - 如何将 CanCan 与 Grape 搭配使用?

c++ - 来自 C++ 的 SQLite 多插入只是添加第一个

android - 新行的损坏插入 ID

.net - 嵌入式系统的数据库

python - python中的多维数组到数据库

database - Cassandra 中的一个节点可靠吗?

javascript - 开发中的 Rails 4 Long Coffeescript 编译时间

sql - Postgres/Rails Active Record——查询四舍五入的浮点值

mysql - 使用 peewee 连接 mysql 以及访问表的一些问题