迁移期间出现“未初始化的常量”错误

时间:2015-07-06 16:02:16

标签: mysql ruby-on-rails ruby

我是Ruby on Rails的新手,我想使用迁移生成一个mysql数据库。

我试过这个命令

ruby bin/rake db:drop db:create db:migrate --trace

但我总是得到这个错误:

    rake aborted!
    StandardError: An error has occurred, all later migrations canceled:

    uninitialized constant CreateOrganisation::Referential
C:/Sites/blog/db/migrate/20120823053740_create_organisation.rb:13:in `up'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:598:in `exec_migration'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/benchmark.rb:281:in `measure'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:578:in `block in migrate'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with
    _connection'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:577:in `migrate'
    C:in `migrate'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:991:in `block in execute_migration_in_transacti
    on'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:1039:in `ddl_transaction'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:952:in `block in migrate'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `each'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:948:in `migrate'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:807:in `up'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/migration.rb:785:in `migrate'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/activerecord-4.1.8/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (req
    uired)>'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `call'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:240:in `block in execute'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `each'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:235:in `execute'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:179:in `block in invoke_with_call_chain'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/2.0.0/monitor.rb:211:in `mon_synchronize'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:172:in `invoke_with_call_chain'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/task.rb:165:in `invoke'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:150:in `invoke_task'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block (2 levels) in top_level'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `each'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:106:in `block in top_level'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:115:in `run_with_threads'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:100:in `top_level'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:78:in `block in run'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:176:in `standard_exception_handling'
    C:/RailsInstaller/Ruby2.0.0/lib/ruby/gems/2.0.0/gems/rake-10.4.2/lib/rake/application.rb:75:in `run'
    bin/rake:4:in `<main>'
    Tasks: TOP => db:migrate

我尝试了命令:

rake db:rollback
rake db:reset
rake db:schema:dump  
rake db:schema:load

但是徒劳无功。

导致错误的文件(我猜)是:

class CreateOrganisation < ActiveRecord::Migration
  def up
    create_table :organisations do |t|
      t.string :name
      t.timestamps
    end
    change_table :referentials do |n|
      n.belongs_to :organisation
    end
    change_table :users do |u|
      u.belongs_to :organisation
    end
    Referential.reset_column_information
    User.reset_column_information

    organisation = Organisation.find_or_create_by_name!("Chouette")
    Referential.update_all :organisation_id => organisation.id
    User.update_all :organisation_id => organisation.id
  end

  def down
    drop_table :organisations
  end
end

我尝试将def change..end代替def up..end/def down..end,但我仍然遇到同样的错误

我看不到我在这里缺少的东西。

1 个答案:

答案 0 :(得分:1)

您正在尝试更改表referential,但它不存在:

change_table :referentials do |n|
  n.belongs_to :organisation
end

错误是指未初始化的常量“:referentials”,它指的是特定的表。

考虑使用生成器创建迁移,直到您了解它们的工作方式;您可以随时调整生成的迁移,或者稍后从头开始编写自己的迁移。

实施例

rails generate migration CreateFoo name:string amount:integer owner:string:index

这将创建

class CreateFoo < ActiveRecord::Migration
  def change
    create_table :foos do |t|
      t.string :name
      t.integer :amount
      t.string :owner
    end
    add_index :foos, :owner
  end
end

参考

我建议您阅读Rails Migration Guide,这有助于您了解如何使用迁移。