0

我正在使用 rails 2.x 版本。我正在使用 mysql 5.0 数据库进行后端进程。当我执行以下步骤时,我遇到了迁移错误。

  1. 从 github 获取应用程序代码。
  2. 运行模式:创建、加载和种子过程
  3. 之后在应用程序中添加一些数据。
  4. 再次从同一分支中提取代码。
  5. 之后我运行 db:migrate 它会引发以下错误
-> rake db:migrate --trace
DEPRECATION WARNING: Rake tasks in vendor/plugins/acts_as_audited/tasks,

vendor/plugins/annotate_models/tasks, vendor/plugins/app_version/tasks, vendor/plugins/bullet/tasks, vendor/plugins/importer/tasks, vendor/plugins/mimetype-fu/tasks, vendor/plugins/railsdav/tasks, vendor/plugins/rav/tasks、vendor/plugins/simple_captcha/tasks、vendor/plugins/smart_table/tasks、vendor/plugins/test_data_generator/tasks、vendor/plugins/visualize_models/tasks 和 vendor/plugins/xss_terminate/tasks 已弃用. 请改用 lib/tasks。(从 /usr/lib/ruby/gems/1.8/gems/rails-2.3.12/lib/tasks/rails.rb:10 调用)

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environmentThu 11 Jul 2013 09:24:36 AM IST
** Execute db:migrate
==  ConvertOrganizationToAwesomeNestedSet: migrating ==========================
Running sample patched rebuild process.
rake aborted!
An error has occurred, all later migrations canceled:

Validation failed: Name can't be blank
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/validations.rb:1102:in

save_without_dirty!' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/dirty.rb:87:in save_without_transactions!/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in save!' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12 /lib/active_record/transactions.rb:182:in transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in save!' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:208:in rollback_active_record_state!' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in save!' /opt/sample/lib/awesome_nested_set_overrides.rb:25:in rebuild!' /opt/sample/lib/awesome_nested_set_overrides.rb:22:incall' /opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!' /opt/ssample/lib/awesome_nested_set_overrides.rb:22:ineach' /opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!' /opt/sample/lib/awesome_nested_set_overrides.rb:32:incall' /opt /sample/lib/awesome_nested_set_overrides.rb:32:in rebuild!' /opt/sample/lib/awesome_nested_set_overrides.rb:29:ineach' /opt/sample/lib/awesome_nested_set_overrides.rb:29:inrebuild!' ./db/migrate//20130102220216_convert_organization_to_awesome_nested_set.rb:6:in up_without_benchmarks' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in send' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in 迁移' /usr/lib/ruby/1.8/benchmark.rb:293:inmeasure' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in 迁移'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:365:in __send__' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:365:in 迁移'/usr/lib/ruby/gems/1.8/gems/activerecord-2.3 .12/lib/active_record/migration.rb:491:in migrate' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:567:in call' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:567:in ddl_transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:490:in migrate' /usr /lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:477:in each' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:477:in migrate' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib /active_record/migration.rb:401:in up' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:383:in 迁移' /usr/lib/ruby/gems/1.8/gems/rails-2.3.12/lib/tasks/databases.rake:112 /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib /rake/task.rb:228:incall' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:228:in执行'/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:223:ineach' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:223:in执行'/usr/lib/ruby /gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:166:in invoke_with_call_chain' /usr/lib/ruby/1.8/monitor.rb:242:insynchronize' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task .rb:159:ininvoke_with_call_chain' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:152:in调用'/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:143:in invoke_task' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in top_level'/usr/lib/ruby/gems/1.8 /gems/rake-10.0.3/lib/rake/application.rb:101:in each' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in top_level' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:110 :在 run_with_threads' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:95:in 顶层'/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb: 73:在run' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:160:in standard_exception_handling' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:70:in run' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/bin/rake:33 /usr/bin/rake:19:inload' /usr/bin/rake:19 任务:TOP => db:migrate

Organization.rebuild!- 这是调用插件的代码。

awesome_nested_set 插件似乎有一些问题。在过去的一周里,我一直在为此苦苦挣扎。任何意见将不胜感激。

编辑 - 1

class ConvertOrganizationToAwesomeNestedSet < ActiveRecord::Migration
 def self.up   
 #  add_column :party, :lft, :integer
 #  add_column :party, :rgt, :integer
      Organization.reset_column_information
      Organization.rebuild!
  end
   def self.down
      remove_column :party, :lft
      remove_column :party, :rgt
  end
end

模型 - 组织

class Organization < Party
attr_accessible :name,
                   :parent_id   
 default_scope :order => 'name'   
 acts_as_nested_set has_many :children, :class_name => 'Organization',
 :foreign_key => "parent_id"  
  belongs_to :parent, :class_name =>'Organization', :foreign_key => "parent_id"  
  def display_name
     name  
   end
4

2 回答 2

1

您在迁移中使用 ActiveRecord 模型类。简而言之:不要。

从长远来看,这很可能会导致问题。如果您有一个使用模型类的迁移,并且稍后再次运行它,那么如果有人更改该模型类,您将遇到麻烦。

迁移严格用于设置数据库,并且应该构造成它们始终以相同的方式运行 - 无论您的应用程序代码的当前状态是什么。

如果您确实需要迁移到 ActiveRecord 模型:在迁移文件中定义该类。这样,对应用程序其余部分的更改将是不可变的。

于 2013-07-11T09:57:43.043 回答
0

既然你得到

Validation failed: Name can't be blank

你必须有一个

validates_presence_of :name

在某个地方,无论是在你的班级还是在插件中。

也许您的数据库中有一些无效记录,当您重建时!它们被保存(并验证)并且您收到此错误。

于 2015-04-15T15:51:46.817 回答