为什么这个简单的 Rails 迁移没有更新我的数据库?

发布于 2024-08-06 20:11:32 字数 679 浏览 0 评论 0原文

我有一个非常简单的迁移,添加了一个布尔列:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海蓝天 2024-08-13 20:11:32

我认为 Rails 没有理由无法添加列。您可能正在查找错误的数据库。

调试此问题的最佳方法是进入 Rails 控制台模式:

script/console development

并创建一个新的首选项对象并给 mute_audio 一个值:

>> p = Preference.new
(...)
>> p.mute_audio = true

在第一个命令之后,您应该看到一些包含有关新创建的对象的信息的输出。您应该看到它有 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:

script/console development

And create a new preference object and give mute_audio a value:

>> p = Preference.new
(...)
>> p.mute_audio = true

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文