Model.reset_column_information不会重新加载在轨迁移列 [英] Model.reset_column_information does not reload columns in rails migration

查看:232
本文介绍了Model.reset_column_information不会重新加载在轨迁移列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用Rails 3.2,并有包含code迁移:

I'm using Rails 3.2 and have a migration that contains the code:

add_column :users, :gift_aid, :integer, :default => 2
# reset columns
User.reset_column_information

... code here to load legacy data from sqlite3 database ...

# now create a user with the loaded column data
user = User.create( ...other cols..., 
                    :gift_aid => migrated_gift_aid_column_data,
                    ...other cols... )

和我得到未知属性:gift_aid 运行迁移时。 User.column_names 显示之前调用 reset_column_information

and I get unknown attribute: gift_aid when running the migration. User.column_names shows the same list before and after the call to reset_column_information.

奇怪的是,当我手动删除列在MySQL并重新运行它工作正常迁移。从第一次迁移再次空数据库启动和它不工作,所以它是与运行的所有迁移,而不是单一的。

Oddly when I manually drop the column in mysql and re-run the migration it works as expected. Starting from the first migration again with an empty database and it doesn't work so it's something to do with running all the migrations rather than the single one.

我对用户模型的几个previous迁移,既包括 reset_column_information 键,都可以。

I have a couple of previous migrations on User model, both include reset_column_information and both work fine.

我真的抓我的头在这一个 - 任何人有任何想法

I'm really scratching my head on this one - anyone got any ideas

推荐答案

我想,这一定是某种缺陷有关架构的缓存...这可能工作:

I think this must be some kind of bug related to schema caching... this might work:

User.connection.schema_cache.clear!
User.reset_column_information

(Rails的3.2.2)

(for Rails 3.2.2)

这篇关于Model.reset_column_information不会重新加载在轨迁移列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆