Rails:无法在迁移中使用 Change_column 添加 : precision 或 :scale 选项?
这似乎已经被问过: 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
有一个相关(但不相同)的问题。我只是改变比例,所以当改变 :scale 时,你需要整行:
省略 :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:
omitting :decimal (which it already was) and :precision (which already was 10) will cause the migration to fail.
不适用于 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.
删除并重新生成
db\schema.rb
文件。Delete and regenerate
db\schema.rb
file.一个 hack,但它应该可以让你到达你需要去的地方:
A hack, but it should get you where you need to go: