通过迁移将默认值添加到列中 [英] Add a default value to a column through a migration

查看:54
本文介绍了通过迁移将默认值添加到列中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将默认值添加到通过迁移已存在的列中?

How do I add a default value to a column that already exists through a migration?

我可以找到的所有文档都向您展示了如果该列不存在但该情况下该如何做的方法.

All the documentation I can find shows you how to do it if the column doesn't already exist but in this case it does.

推荐答案

在这里,您应该这样做:

Here's how you should do it:

change_column :users, :admin, :boolean, :default => false

但是某些数据库(例如PostgreSQL)不会更新以前创建的行的字段,因此请确保在迁移时也手动更新该字段.

But some databases, like PostgreSQL, will not update the field for rows previously created, so make sure you update the field manaully on the migration too.

这篇关于通过迁移将默认值添加到列中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆