如何在 Rails 3 中向小数列添加精度和小数位数而不丢失数据?
我想使用 SQLite 向 Rails 3 中的一些现有小数列添加精度和小数位数。我尝试了 3 种不同的方法,但未能永久影响架构。
首先,我尝试在原始迁移和 schema.rb 中添加精度和小数位数。这看起来很合理,因为现在我的数据库是 SQLite 并且列类型不太重要。但是,下次运行 rake db:migrate 时,精度和小数位数已从 schema.rb 中的这些列中删除。
接下来,我尝试运行一个新的迁移,如下所示:
def up
change_table :my_table do |t|
t.change :my_column, :decimal, precision: 15, scale: 2
end
end
但是,运行此迁移除了更改版本之外对 schema.rb 没有任何影响。所以我尝试了类似的方法:
def up
change_column :my_table, :my_column, :decimal, precision: 15, scale: 2
end
但结果相同:除了版本号之外,没有对 schema.rb 进行任何更改。
如何在不重新运行所有迁移的情况下向小数列添加精度和小数位数?我需要架构能够正确发展,但不会丢失数据。
I want to add precision and scale to some existing decimal columns in Rails 3 with SQLite. I've tried 3 different approaches, but have not been able to permanently affect the schema.
First, I tried just adding the precision and scale in the original migrations and schema.rb. This seemed reasonable since right now my database is SQLite and the column types aren't too important. However, next time I ran rake db:migrate, the precision and scale were removed from those columns in schema.rb.
Next, I tried running a new migration something like this:
def up
change_table :my_table do |t|
t.change :my_column, :decimal, precision: 15, scale: 2
end
end
However, running this migration had no effect on schema.rb except to change the version. So I tried something similar:
def up
change_column :my_table, :my_column, :decimal, precision: 15, scale: 2
end
but with the same result: no change to schema.rb except for the version number.
How can I add precision and scale to my decimal columns without re-running all the migrations? I need the schema to be right going forward, but without losing data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论