在 Ruby on Rails 中更改数据库列的命令

发布于 2024-10-08 11:49:12 字数 333 浏览 0 评论 0原文

是否有一个可以运行的快速控制台命令来更改对象的类型?它目前是 Ruby Date 类型,但我希望它是 Ruby Time 类型。

我从这个脚手架命令开始:

$ rails generate scaffold Post title:string content:text postdate:date

但希望我会执行以下操作:

$ rails generate scaffold Post title:string content:text postdate:time

是否有命令并且可以运行来进行更新?

Is there a quick console command I can run to change the type I have for an object? It's currently a Ruby Date type, but I would like it to be a Ruby Time type.

I started with this scaffold command:

$ rails generate scaffold Post title:string content:text postdate:date

But wish I would have done the following:

$ rails generate scaffold Post title:string content:text postdate:time

Is there a command and can run to make the update?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

橘亓 2024-10-15 11:49:12

有时您必须编写一些实际代码,即使是在 Rails 中也是如此。尝试创建迁移,然后使用 change_column 方法。就像

change_column :my_table, :my_column, :new_type

你把它放在你的数据库迁移文件中,而不是放在 shell 中。

Sometimes you have to write some actual code, even in Rails. Try creating migration and then using change_column method. Something like

change_column :my_table, :my_column, :new_type

You put this in your db migration file, not in shell.

っ〆星空下的拥抱 2024-10-15 11:49:12

如果您不希望错误成为迁移集中的永久部分,只需向下迁移 (rake db:rollback),编辑迁移文件,然后向上迁移 (rake db :迁移)。

编辑:回答你关于只有一个命令的问题?是的,有。编辑迁移后:

rake db:migrate:redo

只需一个命令即可运行“down”,然后运行“up”。

If you don't want the mistake to be a permanent part of the migration set, simply migrate down (rake db:rollback), edit your migration file, and migrate back up (rake db:migrate).

Edit: To answer your question about there being a single command? Yes, there is. After editing your migration:

rake db:migrate:redo

This runs the "down" followed by the "up" in just one command.

看轻我的陪伴 2024-10-15 11:49:12

更改列类型的三个步骤:

步骤 1:

使用此代码生成新的迁移文件:

rails g migration sample_name_change_column_type

步骤 2:

转到 /db/migrate 文件夹并编辑您创建的迁移文件。有两种不同的解决方案。

  1. 定义更改
        更改列(:表名,:列名,:新类型)
    结尾
    

2.

    def up
        change_column :table_name, :column_name, :new_type
    end

    def down
        change_column :table_name, :column_name, :old_type
    end

第 3 步:

不要忘记执行此命令:

rake db:migrate

我已经针对 Rails 4 测试了此解决方案,效果良好。

Three steps to change the type of a column:

Step 1:

Generate a new migration file using this code:

rails g migration sample_name_change_column_type

Step 2:

Go to /db/migrate folder and edit the migration file you made. There are two different solutions.

  1. def change
        change_column(:table_name, :column_name, :new_type)
    end
    

2.

    def up
        change_column :table_name, :column_name, :new_type
    end

    def down
        change_column :table_name, :column_name, :old_type
    end

Step 3:

Don't forget to do this command:

rake db:migrate

I have tested this solution for Rails 4 and it works well.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文