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

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

问题描述

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

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.

推荐答案

你应该这样做:

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天全站免登陆