通过 Rails 迁移更改表信息
在我最初的迁移中,我有这样的:
create_table :credit_purchases do |t|
t.column :amount, :decimal, :precision => 8, :scale => 2, :null => false
t.column :time, :datetime, :null => false
end
产生了以下 MySQL 表定义:
CREATE TABLE `credit_purchases` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(8,2) NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`id`),
)
当我运行这个时,它根本不会改变定义:
change_column :credit_purchases, :amount, :decimal, :precision => 8, :scale => 2
change_column :credit_purchases, :time, :datetime
我期望定义结果是:
CREATE TABLE `credit_purchases` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(8,2) DEFAULT NULL,
`time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
)
我需要做什么才能产生想要的结果?我想避免通过迁移定义数据库约束。
In my original migration, I had this:
create_table :credit_purchases do |t|
t.column :amount, :decimal, :precision => 8, :scale => 2, :null => false
t.column :time, :datetime, :null => false
end
Which produced the following MySQL table definition:
CREATE TABLE `credit_purchases` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(8,2) NOT NULL,
`time` datetime NOT NULL,
PRIMARY KEY (`id`),
)
When I run this, it doesn't change the definition at all:
change_column :credit_purchases, :amount, :decimal, :precision => 8, :scale => 2
change_column :credit_purchases, :time, :datetime
I'd expect the definition result to be:
CREATE TABLE `credit_purchases` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`amount` decimal(8,2) DEFAULT NULL,
`time` datetime DEFAULT NULL,
PRIMARY KEY (`id`),
)
What do I have to do to produce the desired result? I want to avoid defining DB constraints via the migration.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试显式添加
:null =>正确。
Try explicitly adding
:null => true
.