为什么这个简单的 Rails 迁移没有更新我的数据库?
我有一个非常简单的迁移,添加了一个布尔列:
class AddMuteToPreferences < ActiveRecord::Migration
def self.up
add_column :preferences, :mute_audio, :boolean, :default => false
end
def self.down
remove_column :preferences, :mute_audio
end
end
我运行迁移:
== 81 AddMuteToPreferences: migrating =========================================
-- add_column(:preferences, :mute_audio, :boolean, {:default=>false})
-> 1.9043s
== 81 AddMuteToPreferences: migrated (1.9047s) ================================
看起来很漂亮,对吧?但是,由于某种原因,我的首选项表中仍然没有 mute_audio 列。
我想不通。我之前执行过 add_column 没有任何问题。
以前有人见过这种行为吗?
I've got a very simple migration that adds a single boolean column:
class AddMuteToPreferences < ActiveRecord::Migration
def self.up
add_column :preferences, :mute_audio, :boolean, :default => false
end
def self.down
remove_column :preferences, :mute_audio
end
end
I run the migration:
== 81 AddMuteToPreferences: migrating =========================================
-- add_column(:preferences, :mute_audio, :boolean, {:default=>false})
-> 1.9043s
== 81 AddMuteToPreferences: migrated (1.9047s) ================================
Looks peachy, right? But, for some reason, there's still no mute_audio column in my preferences table.
I can't figure it out. I've executed add_column before with no problems.
Has anyone ever seen this behavior before?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 Rails 没有理由无法添加列。您可能正在查找错误的数据库。
调试此问题的最佳方法是进入 Rails 控制台模式:
并创建一个新的首选项对象并给 mute_audio 一个值:
在第一个命令之后,您应该看到一些包含有关新创建的对象的信息的输出。您应该看到它有
mute_autio: false
。如果设置属性 mute_audio 没有输出错误,则没有问题,新添加的列就在那里。I see no reason for rails to fail the add of the column. You are probably looking in the wrong database.
The best way to debug this is to enter rails console mode:
And create a new preference object and give mute_audio a value:
After the first command, you should see some output containing info about the newly created object. You should see that it has
mute_autio: false
. If setting the attribute mute_audio does not output an error, there's no problem and the new added column is there.