Rails:无法在迁移中使用 Change_column 添加 : precision 或 :scale 选项?

发布于 2024-08-24 20:41:43 字数 700 浏览 5 评论 0原文

这似乎已经被问过: rails 小数精度和比例

但是当运行 < code>change_column 迁移 : precision:scale 它们实际上不会影响架构或数据库,但 db:migrate运行没有错误。

我的迁移文件如下所示:

class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration
  def self.up
    change_column :tags, :payback_period, :decimal, { :scale => 3, :precision => 10 }
  end

  def self.down
    change_column :tags, :payback_period, :decimal
  end
end

但我的架构(和数据)仍然为:

t.decimal  "payback_period"

还有其他人有这个问题吗?

谢谢,

乔什

This seems to have been asked before: rails decimal precision and scale

But when running a change_column migration for :precision or :scale they don't actually affect the schema or database, but db:migrate runs without errors.

My migration file looks like this:

class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration
  def self.up
    change_column :tags, :payback_period, :decimal, { :scale => 3, :precision => 10 }
  end

  def self.down
    change_column :tags, :payback_period, :decimal
  end
end

But my schema (and the data) remains as:

t.decimal  "payback_period"

Anybody else have this issue?

Thanks,

Josh

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

情仇皆在手 2024-08-31 20:41:44

有一个相关(但不相同)的问题。我只是改变比例,所以当改变 :scale 时,你需要整行:

change_column :something, :weight, :decimal, :precision => 10, :scale => 2

省略 :decimal (它已经是)和 : precision (已经是 10)将导致迁移失败。

Had a related (but not same) problem. I was just changing scale, so when changing the :scale you need the full line:

change_column :something, :weight, :decimal, :precision => 10, :scale => 2

omitting :decimal (which it already was) and :precision (which already was 10) will cause the migration to fail.

甜扑 2024-08-31 20:41:44

不适用于 SQLite3

对于我正在运行的这个简单的测试应用程序,我有 SQLite3 设置。显然,SQLite3 不依赖于列类型声明,并且更加动态,而是查看列的内容 - 正如在这里偶然发现的:

修改 sqlite3 中的列类型

我还没有测试过它,但我确信这就是架构没有被更改的原因,因为 change_column 不会转换为 SQLite3 中的任何内容。

谢谢你们的回复。

Does Not Work for SQLite3

For this simple test app that I'm running I have SQLite3 setup. Apparently, SQLite3 doesn't rely on column type declarations and is more dynamic, looking at the column's content instead - as was stumbled upon here:

Modify a Column's Type in sqlite3

I haven't tested it but I'm sure that's why the schema wasn't being changed, because change_column doesn't translate to anything in SQLite3.

Thanks for the replies guys.

无可置疑 2024-08-31 20:41:44

删除并重新生成 db\schema.rb 文件。

rake db:schema:dump

Delete and regenerate db\schema.rb file.

rake db:schema:dump
笑叹一世浮沉 2024-08-31 20:41:44

一个 hack,但它应该可以让你到达你需要去的地方:

class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration
  def self.up
    execute "ALTER TABLE tags CHANGE payback_period DECIMAL(3,10)"
  end

  def self.down
    change_column :tags, :payback_period, :decimal
  end
end

A hack, but it should get you where you need to go:

class ChangePrecisionAndScaleOfPaybackPeriodInTags < ActiveRecord::Migration
  def self.up
    execute "ALTER TABLE tags CHANGE payback_period DECIMAL(3,10)"
  end

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