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. Android下拉列表控件spinner-andoid学习之旅(十一)

    废话不多说,下拉列表常用的就是spinner控件. 直接上代码: package peng.liu.testview; import android.app.Activity; import andr ...

  2. Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略

    Activity和Window的View的移动的一些思考与体会,腾讯悬浮小火箭的实现策略 事实上写这个也是因为自己实际在项目中用到了才会去研究已经写文章,对于View的移动,其实说实话,已经有很多文章 ...

  3. Blue Path(基于cocos2dx 3.0)

    猴子原创,欢迎转载.转载请注明: 转载自Cocos2D开发网–Cocos2Dev.com,谢谢! 原文地址: http://www.cocos2dev.com/?p=498 iTunes下载:http ...

  4. 最简单的基于FFmpeg的封装格式处理:视音频复用器(muxer)

    ===================================================== 最简单的基于FFmpeg的封装格式处理系列文章列表: 最简单的基于FFmpeg的封装格式处理 ...

  5. eclipse代码恢复(开发程序代码恢复)

    如果误操作,让本地代码丢失了不用怕,Eclipse local history可以恢复. 误删除文件后,直接ctrl+z可以恢复. 拉去代码覆盖了本地,也可以一个一个或者整体进行恢复:http://b ...

  6. TCP/IP入门(4) --应用层

    /** 本篇博客由汗青ZJF整理并发布, 转载请注明出处: http://blog.csdn.net/zjf280441589/article/category/1854365 */ TCP/IP中的 ...

  7. Mybatis事务(三)事务工厂

    在前面一篇博客Mybatis事务(一)事务管理方式中我们提到,mybatis及spring提供了三个事务实现类:JdbcTransaction.ManagedTransaction和SpringMan ...

  8. 从Perforce到Git的迁移

    公司经过多次兼并.收购之后,开发团队使用的工具自然会出现鱼龙混杂的现象.就拿源代码管理工具来说,我们同时在使用的就有Perforce.Team Foundation.Subversion等.为了节省成 ...

  9. eclipse导入已有工程

    eclipse不同的版本,导致导入已有工程的方法不同.老版本中使用的是新建java工程,然后选择根据已经存在的project创建,就可以了. 但我的是version: Helios Service R ...

  10. (五十八)NSObject实现多线程、自动释放池的补充

    模拟一个图片下载的场景,图片的下载需要2s,在这期间为了保证程序的流畅,应该把图片的下载放在子线程中进行. 使用NSObject的方法performSelectorInBackground方法即可实现 ...