I have the following migration:
我有以下迁移:
def self.up
add_column :project_statuses, :system_sequence, :integer, :default => 0, :null => false
ProjectStatus.create :name => 'Declined', :sequence => 35, :system_sequence => 110
...
end
But when I do a rake db:create
, rake db:migrate
, I get the following error:
但是当我执行rake db:create,rake db:migrate时,我收到以下错误:
== NewProjectStatuses: migrating =============================================
-- add_column(:project_statuses, :system_sequence, :integer, {:default=>0, :null=>false})
-> 0.0029s
rake aborted!
An error has occurred, this and all later migrations canceled:
unknown attribute: system_sequence
/Users/ttt/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1753:in `block in assign_attributes'
/Users/ttt/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1747:in `each'
/Users/ttt/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1747:in `assign_attributes'
/Users/ttt/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:1567:in `initialize'
/Users/ttt/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:508:in `new'
/Users/ttt/.rbenv/versions/1.9.2-p290/lib/ruby/gems/1.9.1/gems/activerecord-3.1.1/lib/active_record/base.rb:508:in `create'
/[working dir]/db/migrate/20100816100139_new_project_statuses.rb:7:in `up'
The erroneous line number refers to the Project.create :name => ...
line.
错误的行号指的是Project.create:name => ...行。
It seems like the add_column
line isn't run at all, even though the output says it is run.
似乎add_column行根本没有运行,即使输出显示它已运行。
Running a rake db:migrate
again runs through the migrations fine though.
运行rake db:migrate再次可以完成迁移。
Why is this?
为什么是这样?
1 个解决方案
#1
16
Try calling ProjectStatus.reset_column_information
after the add_column
block.
尝试在add_column块之后调用ProjectStatus.reset_column_information。
Rails caches column information (including which columns exist), and I believe what you're hitting here is that you're creating a column, but that doesn't trigger Rails to reset its cache on the list of available columns. As a result, your following line fails, because it doesn't think the column exists.
Rails缓存列信息(包括存在哪些列),我相信你在这里遇到的是你正在创建一个列,但这并不会触发Rails重置其可用列列表中的缓存。因此,您的以下行失败,因为它不认为该列存在。
I'm not sure why the caching is designed this way, but this sort of thing is explicitly mentioned in the example code regarding the use of reset_column_information
. I think there's a pretty good chance that's the issue.
我不确定为什么缓存是这样设计的,但是在示例代码中明确提到了有关reset_column_information的使用。我认为这是一个非常好的机会。
That being said, I also generally agree with Michael Durrant, regarding the use of filling the DB with values via migrations. The preferred method is to add your default/seed data to a rake task (which can be run at any arbitrary time), or your seeds.rb
file. This is in contrast to a migration, which is only run when the current schema version is older than the specified migration.
话虽这么说,我也普遍同意Michael Durrant关于使用通过迁移为数据库填充数据的方法。首选方法是将默认/种子数据添加到rake任务(可以在任意时间运行)或seeds.rb文件中。这与迁移形成对比,迁移仅在当前模式版本早于指定迁移时运行。
#1
16
Try calling ProjectStatus.reset_column_information
after the add_column
block.
尝试在add_column块之后调用ProjectStatus.reset_column_information。
Rails caches column information (including which columns exist), and I believe what you're hitting here is that you're creating a column, but that doesn't trigger Rails to reset its cache on the list of available columns. As a result, your following line fails, because it doesn't think the column exists.
Rails缓存列信息(包括存在哪些列),我相信你在这里遇到的是你正在创建一个列,但这并不会触发Rails重置其可用列列表中的缓存。因此,您的以下行失败,因为它不认为该列存在。
I'm not sure why the caching is designed this way, but this sort of thing is explicitly mentioned in the example code regarding the use of reset_column_information
. I think there's a pretty good chance that's the issue.
我不确定为什么缓存是这样设计的,但是在示例代码中明确提到了有关reset_column_information的使用。我认为这是一个非常好的机会。
That being said, I also generally agree with Michael Durrant, regarding the use of filling the DB with values via migrations. The preferred method is to add your default/seed data to a rake task (which can be run at any arbitrary time), or your seeds.rb
file. This is in contrast to a migration, which is only run when the current schema version is older than the specified migration.
话虽这么说,我也普遍同意Michael Durrant关于使用通过迁移为数据库填充数据的方法。首选方法是将默认/种子数据添加到rake任务(可以在任意时间运行)或seeds.rb文件中。这与迁移形成对比,迁移仅在当前模式版本早于指定迁移时运行。