为什么我在迁移过程中会收到错误以及默认数据?

发布于 2024-11-05 23:29:19 字数 1357 浏览 0 评论 0原文

    rails g migration CreateStates

然后将以下代码添加到迁移中:

=============================

class CreateStates < ActiveRecord::Migration
  def self.up
    create_table :states do |t|
      t.column :name, :string
      t.column :abbreviation, :string
    end

    State.create :name => 'Alabama', :abbreviation => 'AL'
    State.create :name => 'Alaska', :abbreviation => 'AK'
    State.create :name => 'Arizona', :abbreviation => 'AZ'
  end

  def self.down
    drop_table :states
  end
end

============== ==============

我收到错误:

** Invoke db:migrate (first_time)
** 调用环境(first_time)
** 执行环境
** 执行 db:migrate
== CreateStates: 迁移 ================================================= ======
-- create_table(:states)
-> 0.0010s
耙子中止!
发生错误,此迁移和所有后续迁移均已取消:

未初始化常量 CreateStates::State
/Users/jondoe/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in ` const_missing'

========

看起来应该可以做到这一点:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

我还尝试创建一个模型而不仅仅是一个迁移文件。还是同样的错误。我还尝试创建 2 个迁移(一个用于创建表,一个用于添加数据),但这也不起作用。有什么想法吗?

    rails g migration CreateStates

Then add the following code to the migration:

===========================

class CreateStates < ActiveRecord::Migration
  def self.up
    create_table :states do |t|
      t.column :name, :string
      t.column :abbreviation, :string
    end

    State.create :name => 'Alabama', :abbreviation => 'AL'
    State.create :name => 'Alaska', :abbreviation => 'AK'
    State.create :name => 'Arizona', :abbreviation => 'AZ'
  end

  def self.down
    drop_table :states
  end
end

============================

I get an error:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:migrate
== CreateStates: migrating ===================================================
-- create_table(:states)
-> 0.0010s
rake aborted!
An error has occurred, this and all later migrations canceled:

uninitialized constant CreateStates::State
/Users/jondoe/.rvm/rubies/ruby-1.8.7-p330/lib/ruby/gems/1.8/gems/rspec-core-2.5.1/lib/rspec/core/backward_compatibility.rb:20:in `const_missing'

========

It seems like this should be able to do this:
http://api.rubyonrails.org/classes/ActiveRecord/Migration.html

I have also tried to create a model instead of a just a migration file. Still same error. I have also tried creating 2 migrations (one to create table, then one to add data) and that didn't work either. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

樱花细雨 2024-11-12 23:29:19

尝试

State.reset_column_information

在 State.create 之前

执行以下操作: 文档

Try doing:

State.reset_column_information

before your State.create.

documentation

巴黎夜雨 2024-11-12 23:29:19

您的代码将完美运行,除非您实际上没有 State 类。 Rails 了解此类的唯一方法是在 app/models/state.rb 中将其定义为 Class State Class State ActiveRecord::Base...

我建议运行以下代码行,而不是运行自定义迁移:

rails g model State name:string abbreviation:string

这将:

  1. 创建模型(和单元测试文件)
  2. 创建一个名为 20110508223913_create_states 的迁移.rb,它看起来与您上面尝试的迁移几乎相同。

然后您需要做的就是添加您的 State.create... 行,然后就可以开始了。

Your code would work perfectly, except that you don't actually have a State class. The only way Rails would know about this class is if you defined it in app/models/state.rb as Class State < ActiveRecord::Base...

Rather than running a custom migration, I'd recommend running this line of code:

rails g model State name:string abbreviation:string

This will:

  1. Create your model (and unit test files)
  2. Create a migration named something like 20110508223913_create_states.rb, which will look nearly identical to your attempted migration above.

Then all you need to do is add your State.create... lines and you should be good to go.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文