Rails 迁移变更列
我们使用脚本/生成迁移 add_fieldname_to_tablename fieldname:datatype 语法来向模型添加新列。
在同一行上,我们是否有一个脚本/生成来更改列的数据类型?或者我应该将 SQL 直接写入我的普通迁移中?
我想将列从 datetime
更改为 date
。
We have script/generate migration add_fieldname_to_tablename fieldname:datatype
syntax for adding new columns to a model.
On the same line, do we have a script/generate for changing the datatype of a column? Or should I write SQL directly into my vanilla migration?
I want to change a column from datetime
to date
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(12)
使用#change_column。
注意:即使在数据库迁移之外也可以实现相同的结果,这对于测试/调试可能很方便,但需要非常谨慎地使用此方法:
Use #change_column.
NOTE: the same result can be achieved even outside of db migrations, this might be handy for testing/debugging but this method needs to be used very cautiously:
如果表中有多个列需要更改,您也可以使用块。
示例:
有关更多详细信息,请参阅有关 Table 类的 API 文档 。
You can also use a block if you have multiple columns to change within a table.
Example:
See the API documentation on the Table class for more details.
我不知道您是否可以从命令行创建迁移来完成所有这些操作,但您可以创建一个新的迁移,然后编辑该迁移来执行此任务。
如果 tablename 是表的名称,fieldname 是字段的名称,并且您想要从日期时间更改为日期,则可以编写迁移来执行此操作。
您可以使用以下方法创建新的迁移:
然后编辑迁移以使用change_table:
然后运行迁移:
I'm not aware if you can create a migration from the command line to do all this, but you can create a new migration, then edit the migration to perform this taks.
If tablename is the name of your table, fieldname is the name of your field and you want to change from a datetime to date, you can write a migration to do this.
You can create a new migration with:
Then edit the migration to use change_table:
Then run the migration:
正如我在前面的答案中发现的,更改列的类型需要三个步骤:
步骤 1:
使用此代码生成新的迁移文件:
步骤 2:
转到
/db/migrate
文件夹并编辑您制作的迁移文件。有两种不同的解决方案。2.
第 3 步:
不要忘记执行此命令:
我已经针对 Rails 4 测试了此解决方案,效果良好。
As I found by the previous answers, three steps are needed to change the type of a column:
Step 1:
Generate a new migration file using this code:
Step 2:
Go to
/db/migrate
folder and edit the migration file you made. There are two different solutions.2.
Step 3:
Don't forget to do this command:
I have tested this solution for Rails 4 and it works well.
对于 Rails 5,
来自 Rails 指南:
With Rails 5
From Rails Guides:
只需生成迁移:
像这样更新迁移:
最后
Just generate migration:
Update migration like this:
and finally
这一切都假设列的数据类型对任何现有数据都有隐式转换。我遇到过几种情况,现有数据(比如说
String
)可以隐式转换为新的数据类型(比如说Date
)。在这种情况下,了解可以通过数据转换创建迁移会很有帮助。就我个人而言,我喜欢将它们放入我的模型文件中,然后在所有数据库模式已迁移并稳定后将其删除。
This is all assuming that the datatype of the column has an implicit conversion for any existing data. I've run into several situations where the existing data, let's say a
String
can be implicitly converted into the new datatype, let's sayDate
.In this situation, it's helpful to know you can create migrations with data conversions. Personally, I like putting these in my model file, and then removing them after all database schemas have been migrated and are stable.
您可以使用
change_column
为此:You can use
change_column
for this:要在编辑默认值时完成答案:
在 Rails 控制台中:
在迁移中:
将如下所示:
To complete answers in case of editing default value :
In your rails console :
In the migration :
Will look like :
使用迁移更改数据类型的另一种方法
第 1 步:
您需要使用迁移删除错误的数据类型字段名称
ex:
这里不要忘记为您的字段指定数据类型
第2步:
现在您可以添加具有正确数据类型的字段
ex:
就是这样,现在您的表将添加正确的数据类型字段,快乐的 ruby 编码!
Another way to change data type using migration
step1:
You need to remove the faulted data type field name using migration
ex:
Here don't forget to specify data type for your field
Step 2:
Now you can add field with correct data type
ex:
That's it, now your table will added with correct data type field, Happy ruby coding!!
使用 Postgresql,您可能会收到错误,因为 Postgresql 需要有关如何将值转换为另一种类型的特定说明。这可以通过 CAST 来实现。您可能需要编写单独的“向上”和“向下”迁移,因为 Rails 无法反转 CAST 语句。例如,将字符串字段转换为文本:
有关 CAST 的更多信息,请参见 https:// /www.postgresql.org/docs/8.3/sql-expressions.html。
With Postgresql you might get an error because Postgresql needs specific instructions on how to cast values into another type. This can be achieved with CAST. You probably need to write separate "up" and "down" migrations because Rails cannot reverse the CAST statement. For example to cast a string field into text:
More info on CAST in https://www.postgresql.org/docs/8.3/sql-expressions.html.
这种格式可以帮助我:
That format helps me: