Rails迁移错误 - 名称不能为空

时间:2013-07-11 04:08:01

标签: ruby-on-rails database-migration ruby-on-rails-2 rails-migrations

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

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

    供应商/插件/ annotate_models /任务,   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,   供应商/插件/ test_data_generator /任务,   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 交易”       /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 救人!”       /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 救人!”       /opt/sample/lib/awesome_nested_set_overrides.rb:25:in rebuild!' /opt/sample/lib/awesome_nested_set_overrides.rb:22:in call'       /opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!' /opt/ssample/lib/awesome_nested_set_overrides.rb:22:in每个'       /opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!' /opt/sample/lib/awesome_nested_set_overrides.rb:32:in call'       /opt/sample/lib/awesome_nested_set_overrides.rb:32:in rebuild!' /opt/sample/lib/awesome_nested_set_overrides.rb:29:in每个'       /opt/sample/lib/awesome_nested_set_overrides.rb:29:in rebuild!' ./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:in measure' /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in migrate'       /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 称之为”       /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 迁移”       /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 迁移”       /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:in call' /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:in each' /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:in synchronize'       /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:159:in invoke_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:in   run_with_threads' /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:95:in TOP_LEVEL”       /usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:73:in   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:in负载”       在/ usr / bin中/耙:19       任务:TOP =>分贝:迁移

    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
    

2 个答案:

答案 0 :(得分:1)

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

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

迁移严格用于设置数据库,并且应该构建为使它们始终以相同的方式运行 - 无论应用程序代码的当前状态如何。

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

答案 1 :(得分:0)

因为你正在

Validation failed: Name can't be blank

你必须有一个

validates_presence_of :name

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

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