rails项目中由于后期需求变化,需要在products数据库中增加一个字段来满足多国家商品的分类:

rails g migration add_locale_to_products locale:string

完了之后,正常的web app操作均正常,但是末了测试时发生异常:

rake test

打印出一大坨错误 :( ,进入test.sqlite3数据库中没有看到新增的字段.遂使用如下命令强制将数据库更改应用到test数据库:

RAILS_ENV=test rake db:seed

但是还是抛出异常,错误代码依旧:

wisy@wisy-ThinkPad-X61:~/src/ruby_src/rails_test/depot$ RAILS_ENV=test rake db:migrate
rake 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)

仔细查看错误代码,发现在seeds.rb中出错,其对应的代码是手动添加一个product对象:

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

想了一下,这个大概构成了一个死循环:rake db:migrate需要将locale字段加到products中去,但是首先会执行seeds.rb中的Product.create这里locale却还没有呢!

所以现将create代码注释掉,然后执行RAILS_ENV=test rake db:seed,一切正常;接着将该注释的代码重新打开,最后执行rake test,这时原先的异常没有了,取而代之的是几个assert出现F!

首先将products.yml中model新增的locale字段补全:

one:
  title: MyString
  description: MyText
  image_url: /images/one.jpg
  price: 9.99
  locale: 'cn'

然后检查发送F的方法,发现大多都是缺少应用新locale字段的原因,依次添加locale字段即可.

rails中migration数据库后测试不通过的问题的更多相关文章

  1. ubuntu14.04使用rails连接mysql数据库

    rails自带的sqlite3各方面都不错,但是免费版缺少一个致命功能:加密码!虽说第三方有编译好的二进制版的加密版,但咱先不折腾鸟;直接上mysql吧. ubuntu安装mysql非常简单,先不聊; ...

  2. MySql数据库在表中添加新字段,设置主键,设置外键,字段移动位置,以及修改数据库后如何进行部署和维护的总结

    1,为当前已有的表添加新的字段 alter table student add studentName varchar(20) not null; 2,为当前已有的表中的字段设置为主键自增 alter ...

  3. 在mysql数据库中制作千万级测试表

    在mysql数据库中制作千万级测试表 前言: 最近准备深入的学一下mysql,包括各种引擎的特性.性能优化.分表分库等.为了方便测试性能.分表等工作,就需要先建立一张比较大的数据表.我这里准备先建一张 ...

  4. EF Core中,通过实体类向SQL Server数据库表中插入数据后,实体对象是如何得到数据库表中的默认值的

    我们使用EF Core的实体类向SQL Server数据库表中插入数据后,如果数据库表中有自增列或默认值列,那么EF Core的实体对象也会返回插入到数据库表中的默认值. 下面我们通过例子来展示,EF ...

  5. Mybatis在oracle数据库中插入数据后返回自增值ID

    1.将id设置成自增序列 CREATE OR REPLACE TRIGGER "DATALIB"."TRIG_USER_ADD" BEFORE INSERT O ...

  6. 用texarea存储数据,查询数据库后原样显示在jsp中,包括空格和回车换行

    用texarea存储数据,查询数据库后原样显示在jsp中,包括空格和回车换行

  7. tp5中使用ueditor编辑器保存文本到数据库后回显后显示html标签问题解决办法

    在编辑器ueditor中获取文本,保存到到数据库后为 当在数据库中提取出来,在显示回ueditor编辑器时候,出了问题, html标签都显示出来了 百度了下别人的解决办法是,使用官方提供的api 可是 ...

  8. django模板中变更数据库信息后,如何把变更后的信息同步更新到数据库

    我们在基于django开发项目的过程中,经常会遇到数据库表字段增加,删除,或者修改的情况,以及字段属性更改的情况,因为django基于ORM模式来操作数据库的, 传统上如果django项目中的数据库m ...

  9. 生产环境中,数据库升级维护的最佳解决方案flyway

    官网:https://flywaydb.org/ 转载:http://casheen.iteye.com/blog/1749916 1.  引言 想到要管理数据库的版本,是在实际产品中遇到问题后想到的 ...

随机推荐

  1. Storm 0.9安装指南

    Storm 0.9.2安装指南 0 Storm0.9的亮点 引用网上的描述: "Storm 0.9.0.1版本的第一亮点是引入了netty transport.Storm网络传输机制实现可插 ...

  2. linux下查看Memcached运行状态

    查看Memcached运行状态的命令是:echo stats | nc 127.0.0.1 11211 查看memcached状态的基本命令,通过这个命令可以看到如下信息: STAT pid 2245 ...

  3. CentOs查看文件的几种方式

    有许多命令都可以查看文件,不同的命令有不同的优点,可以针对不同的需要分别选择命令以提高效率:   cat     由第一行开始显示内容,并将所有内容输出   tac     从最后一行倒序显示内容,并 ...

  4. 参数估计:最大似然估计MLE

    http://blog.csdn.net/pipisorry/article/details/51461997 最大似然估计MLE 顾名思义,当然是要找到一个参数,使得L最大,为什么要使得它最大呢,因 ...

  5. Android计时器Chronometer-android学习之旅(二十一)

    Chronometer简介 Chronometer和DigitalColok都继承与TextView,但是Chronometer不是显示的当前时间,而是从某个时间开始又过去了多少时间,是一个时间差. ...

  6. 简单RPC实现之Netty实现

    所谓RPC就是远程方法调用(Remote  Process Call ),简单的来说就是通过MQ,TCP,HTTP或者自己写的网络协议来传输我要调用对方的什么接口,对方处理之后再把结果返回给我.就这么 ...

  7. android git上开源的项目收藏

    本文为那些不错的Android开源项目第一篇--个性化控件(View)篇,主要介绍Android上那些不错个性化的View,包括ListView.ActionBar.Menu.ViewPager.Ga ...

  8. 给EditText的drawableRight属性的图片设置点击事件

    这个方法是通用的,不仅仅适用于EditText,也适用于TextView.AutoCompleteTextView等控件. Google官方API并没有给出一个直接的方法用来设置右边图片的点击事件,所 ...

  9. java实现异步调用实例

    在JAVA平台,实现异步调用的角色有如下三个角色: 调用者 取货凭证   真实数据 一个调用者在调用耗时操作,不能立即返回数据时,先返回一个取货凭证.然后在过一断时间后凭取货凭证来获取真正的数据.  ...

  10. Xcode中Objc动态调用方法同时避免警告的几个办法

    我们在Xcode中使用objc写代码的时候往往会碰到动态调用方法的时候. 如果是静态调用这很常见,不会有任何问题: [self performSelector:@selector(method)]; ...