通过迁移在 ruby​​-on-rails 3 中创建表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3031457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:47:01  来源:igfitidea点击:

creating tables in ruby-on-rails 3 through migrations?

ruby-on-rails

提问by never_had_a_name

im trying to understand the process of creating tables in ruby-on-rails 3.

我试图了解在 ruby​​-on-rails 3 中创建表的过程。

i have read about migrations. so i am supposed to create tables by editing in the files in:

我已经阅读了有关迁移的信息。所以我应该通过编辑以下文件来创建表:

Database Migrations/migrate/20100611214419_create_posts
Database Migrations/migrate/20100611214419_create_categories

but they were generated by:

但它们是由以下生成的:

rails generate model Post name:string description:text
rails generate model Category name:string description:text

does this mean i have to use "rails generate model" command everytime i want to create a table?

这是否意味着我每次要创建表时都必须使用“rails generate model”命令?

what if i create a migration file but want to add columns. do i create another migration file for adding those or do i edit the existing migration file directly? the guide told me to add a new one, but here is the part i dont understand. why would i add a new one? cause then the new state will be dependent of 2 migration files.

如果我创建了一个迁移文件但想添加列怎么办。我是创建另一个迁移文件来添加这些文件还是直接编辑现有的迁移文件?指南告诉我添加一个新的,但这是我不明白的部分。为什么我要添加一个新的?因为那么新状态将依赖于 2 个迁移文件。

and how do i add a new migration file for updating then? what is the command? and if i have to drop columns or edit them. how do it do that?

以及如何添加新的迁移文件进行更新?命令是什么?如果我必须删除列或编辑它们。它是怎么做到的?

rails generate model Post name:string description:text

cause the above command just add columns.

因为上面的命令只是添加列。

and if i don't use the commands, how do i create migration files?

如果我不使用这些命令,我​​该如何创建迁移文件?

in symfony i just edit a schema.yml file directly, there are no migration files with versioning and so on.

在 symfony 中,我只是直接编辑 schema.yml 文件,没有带有版本控制等的迁移文件。

and i think in django you just create the models and it will create the database tables.

我认为在 django 中你只需创建模型,它就会创建数据库表。

im new to RoR and want to get the picture of creating tables.

我是 RoR 的新手,想要了解创建表格的过程。

thanks

谢谢

回答by Tomas Markauskas

If you want to update a table you have to create a new migration file because each migrations is executed only once on the database. So if you already have a poststable then after modifying the create_postsmigration you won't be able to run it again.

如果你想更新一个表,你必须创建一个新的迁移文件,因为每次迁移只在数据库上执行一次。因此,如果您已经有一个posts表,那么在修改create_posts迁移后您将无法再次运行它。

You can rollback migrations and then run them again. That would solve the problem but it would also destroy the table and the data that it might hold. This isn't a problem if you just created the migration and then noticed that you missed one column. Then you can just add the column to the migration, rollback and migrate. But you don't want to do that on a production database!

您可以回滚迁移,然后再次运行它们。这将解决问题,但也会破坏表及其可能保存的数据。如果您刚刚创建迁移,然后注意到您错过了一列,这不是问题。然后,您可以将列添加到迁移、回滚和迁移中。但是您不想在生产数据库上这样做!

To create a new migration you just run:

要创建新的迁移,您只需运行:

rails generate migration migration_name

If you call your migration add_*_to_tablethen you can also pass the same arguments as in generate model:

如果您调用迁移,add_*_to_table那么您还可以传递与中相同的参数generate model

rails generate migration add_something_to_posts something:boolean

This will automatically generate this migration:

这将自动生成此迁移:

class AddSomethingToPosts < ActiveRecord::Migration
  def self.up
    add_column :posts, :something, :boolean
  end

  def self.down
    remove_column :posts, :something
  end
end

This will work with remove_*_from_tabletoo:

这也适用于remove_*_from_table

rails generate migration remove_something_from_posts something:boolean

The migration will be:

迁移将是:

class RemoveSomethingFromPosts < ActiveRecord::Migration
  def self.up
    remove_column :posts, :something
  end

  def self.down
    add_column :posts, :something, :boolean
  end
end

Here are some more methodsthat you can use in your migrations.

以下是您可以在迁移中使用的更多方法

回答by Garrett

To generate stand alone migration files, you'll use:

要生成独立的迁移文件,您将使用:

rails generate migration your_migration_name_here

Once you go to the generated file you can execute any SQL you like, looking at the other generated migrations will give you a good starting point as well.

转到生成的文件后,您可以执行任何您喜欢的 SQL,查看其他生成的迁移也将为您提供一个很好的起点。

Generally in Rails, you have a model for every table you have in your database. I can't think of any specific use cases where you'll have a table without some sort of model to access that data. If you want to get an in depth look of migrations, go here.

通常在 Rails 中,数据库中的每个表都有一个模型。我想不出任何特定的用例,您将拥有一个没有某种模型来访问该数据的表。如果您想深入了解迁移,请转到此处

回答by RSK

Regarding this question Does this mean I have to use rails generate modelcommand everytime I want to create a table?

关于这个问题 这是否意味着我rails generate model每次要创建表时都必须使用命令?

Try migration_for
I find this helpful for me. Find the instruction on author's blog

尝试迁移
_因为我发现这对我有帮助。在作者的博客上找到说明

rails g migration_for create_table:posts add_column:posts:title:string add_column:posts:user_id:integer add_index:posts:user_id