Problems with migration database after testing in rails

Source: Internet
Author: User
Tags locale

In the Rails project, due to late demand changes, you need to add a field in the products database to meet the classification of multi-country commodities:

rails g migration add_locale_to_products locale:string

After that, normal web app operations are normal, but an exception occurred at the end of the test:

rake test

Print out a big lump of error:(, no new fields are seen in the Test.sqlite3 database. The following command is used to force the database changes to be applied to the test database:

RAILS_ENV=test rake db:seed

However, the exception is thrown, and the error code remains:

[Email Protected]:~/src/ruby_src/rails_test/depot$ RAILS_env=test Rake Db:migraterake aborted! Activerecord::unknownattributeerror:unknown attribute ' locale ' for product./var/lib/gems/2.1.0/gems/ Activerecord-4.2.0/lib/active_Record/attribute_assignment.rb:59:in ' Rescue in _Assign_attribute '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_Record/attribute_assignment.rb:54:in ' _Assign_attribute '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_Record/attribute_assignment.rb:41:in ' block in Assign_Attributes '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:35:in' Each '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:35:in' Assign_attributes '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in ' Init_Attributes '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in ' Initialize '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_Record/inheritance.rb:61:in' new '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in ' new '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_Record/persistence.rb:33:in' Create 'Db/seeds.rb:11:in' <top (required) > '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' Load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/dependencies.rb:268:in' block in Load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in ' Load_Dependency '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' Load '/home/wisy/src/ruby_Src/rails_test/depot/config/initializers/make_Payment_types_When_test.rb:2:in ' <top (required) > '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/dependencies.rb:268:in' Load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' block in load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/dependencies.rb:240:in' load_dependency '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' load '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:652:in ' Block in Load_Config_initializer '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/notifications.rb:166:in' instrument '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:651:in' Load_config_initializer '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:616:in' block (2 levels) in <class:Engine> '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in' Each '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in' block in <class:Engine> '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in' Instance_exec '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in' Run '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in' block in Run_initializers '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in' Each '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in' Tsort_each_child '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in' Run_initializers '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:352:in' initialize! '/home/wisy/src/ruby_src/rails_Test/depot/config/environment.rb:5:in' <top (required) > '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in' Require '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in' require_environment! '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:443:in' block in Run_tasks_blocks 'Nomethoderror:undefined method' locale= 'For #<product:0x00000004c7fe30>/var/lib/gems/2.1.0/gems/activemodel-4.2.0/lib/active_model/attribute_Methods.rb:433:in' method_missing '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:54:in' Public_send '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:54:in' _assign_attribute '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:41:in' block in Assign_attributes '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:35:in' Each '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/attribute_Assignment.rb:35:in' Assign_attributes '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:557:in ' Init_Attributes '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/core.rb:280:in ' Initialize '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_Record/inheritance.rb:61:in' new '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_record/inheritance.rb:61:in ' new '/var/lib/gems/2.1.0/gems/activerecord-4.2.0/lib/active_Record/persistence.rb:33:in' Create 'Db/seeds.rb:11:in' <top (required) > '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' Load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/dependencies.rb:268:in' block in Load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in ' Load_Dependency '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' Load '/home/wisy/src/ruby_Src/rails_test/depot/config/initializers/make_Payment_types_When_test.rb:2:in ' <top (required) > '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/dependencies.rb:268:in' Load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' block in load '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/dependencies.rb:240:in' load_dependency '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in ' load '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:652:in ' Block in Load_Config_initializer '/var/lib/gems/2.1.0/gems/activesupport-4.2.0/lib/active_Support/notifications.rb:166:in' instrument '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:651:in' Load_config_initializer '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:616:in' block (2 levels) in <class:Engine> '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in' Each '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/engine.rb:615:in' block in <class:Engine> '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in' Instance_exec '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:30:in' Run '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:55:in' block in Run_initializers '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in' Each '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:44:in' Tsort_each_child '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/initializable.rb:54:in' Run_initializers '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:352:in' initialize! '/home/wisy/src/ruby_src/rails_Test/depot/config/environment.rb:5:in' <top (required) > '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in' Require '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:328:in' require_environment! '/var/lib/gems/2.1.0/gems/railties-4.2.0/lib/rails/application.rb:443:in' block in Run_tasks_blocks 'Tasks:top = Db:migrate = Environment (see full trace by running task with--trace)

Look closely at the error code and find an error in seeds.rb, and the corresponding code is to manually add a Product object:

Product.delete_allProduct.create(title:"iphone6 plus",  description:"iphone6 plus目前地球上最好的手机,没有之一!!!",  image_url:‘/images/p6p.jpg‘,  price:599.50,  locale:‘cn‘)

Thinking about it, this probably constitutes a dead loop: Rake db:migrate needs to add the locale field to the products, but first executes the product.create in seeds.rb where the locale is not yet!
So now the Create code is commented out, and then executed rails_env=test rake db:seed, everything is OK, then the code of the comment reopened, finally executed rake test, then the original exception is not, instead of a few assert the presence of f!
First, complete the new locale field in the PRODUCTS.YML model:

one:  title: MyString  description: MyText  image_url: /images/one.jpg  9.99  ‘cn‘

Then check the method of sending F, and find that most of them are missing the reason to apply the new locale field, then add the locale field in turn.

Problems with migration database after testing in rails

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.