简体   繁体   English

为什么在向表中添加新列时不再能耙db:migrate了? 我可以在迁移文件中手动添加列还是应该生成一个?

[英]Why can I no longer rake db:migrate when adding new columns to my table? Can I add columns manually in the migration file or should I generate one?

Good morning everyone, I am trying to complete the calender app from Codeacademy, and I need to complete the following steps. 大家早上好,我正在尝试从Codeacademy完成日历应用程序,我需要完成以下步骤。

Open the migration file in db/migrate/ for the days table, and add the following columns: 在db / migrate /中为days表打开迁移文件,然后添加以下列:

  • a datetime column called date 日期时间列,称为日期

Open the migration file in db/migrate/ for the days table, and add the following columns: a datetime column called date Open the migration file in db/migrate/ for events tracks table, and add the following columns: 在db / migrate /中为days表打开迁移文件,并添加以下列:一个名为date的datetime列在db / migrate /中为事件跟踪表打开迁移文件,并添加以下列:

  1. a string column called name 名为name的字符串列
  2. a datetime column called from 从中调用的日期时间列
  3. a datetime column called to 日期时间列称为
  4. a string column called location 称为位置的字符串列
  5. a references column to the Day model Day模型的参考列

So far, I have added it manually to the migration files like so: 到目前为止,我已经将其手动添加到了迁移文件中,如下所示:

class CreateDays < ActiveRecord::Migration
  def change
    create_table :days do |t|
      t.datetime :date
      t.timestamps
    end
  end
end

class CreateEvents < ActiveRecord::Migration
  def change
    create_table :events do |t|
      t.string :name
      t.datetime :from 
      t.datetime :to 
      t.string :location 
      t.timestamps
      #Not sure how to add references column????
    end
  end
end

However, I am running into an error when I run rake db:migrate, I get no output. 但是,当我运行rake db:migrate时遇到错误,但是没有任何输出。 Is there supposed to be an output? 是否应该有输出? I have run rake db:migrate --trace and here is the output: 我已经运行rake db:migrate --trace,这是输出:

A migration file is like a script which changes your database in some way. 迁移文件就像一个脚本 ,它以某种方式更改数据库。 It does not store the state of your database. 它不存储数据库的状态。

It will not do anything unless you run it: your database has a special table to keep track of which ones have been run already, called schema_migrations . 除非您运行它,否则它不会做任何事情:数据库具有一个特殊的表,用于跟踪已运行的表,称为schema_migrations When you do rake db:migrate you run all scripts in that folder which haven't been run already, according to that table. 根据该表,在执行rake db:migrate运行该文件夹中尚未运行的所有脚本。

So, if you run a script, then change it, then do db:migrate, it won't run it again because it thinks it's been run already. 因此,如果您运行一个脚本,然后对其进行更改,然后再执行db:migrate,它将不会再次运行它,因为它认为它已经运行了。 If it did run it again, it would likely blow up because it would be trying to add lots of columns that already exist. 如果没有再次运行它,它可能会炸掉,因为它会尝试添加大量已经存在的列。

If you define a table in a migration, then later want to add more columns, you can either roll the migration back (which will drop the table), then run it again with the added columns, or write a new migration which just adds the required new columns. 如果您在迁移中定义了一个表,然后又想添加更多列,则可以回滚迁移(这将删除该表),然后使用添加的列再次运行它,或者编写一个新迁移,仅添加需要新的列。 The latter approach is usually best. 后一种方法通常是最好的。

I assume that you have ran the migrations before. 我假设您之前已经进行过迁移。 So there is a table named schema_migrations where it stores the migration which has already ran so you cannot re run them. 因此,存在一个名为schema_migrations的表,其中存储了已经运行的迁移,因此您无法重新运行它们。 The best way to add new columns to an existing table is to create a new migration like this: 将新列添加到现有表的最佳方法是创建一个新的迁移,如下所示:

rails g migration add_some_columns_to_events name:string

and name the other columns. 并命名其他列。 And still if you need to do it using the existing migration then what you can do is: 仍然,如果您需要使用现有的迁移来做到这一点,那么您可以做的是:

rake db:migrate:down VERSION=<version_of_migration>

and then add up the columns in migration file and run: 然后将迁移文件中的列加起来并运行:

rake db:migrate:up VERSION=<version_of_migration>

You can also rollback your last migration and re run the migration using: 您还可以回滚上一次迁移并使用以下命令重新运行迁移:

rake db:rollback

Hope this helps. 希望这可以帮助。

You can only run a migration once - if you've already run the migration, you have two options: 您只能运行一次迁移-如果您已经运行了迁移,则有两个选择:

  • You can roll back the migration with rake db:rollback , edit it, and then run it again with rake db:migrate , or 您可以回滚与迁移rake db:rollback ,编辑它,然后再次运行rake db:migrate ,或
  • You can generate a new migration and put your changes in it. 您可以生成一个新的迁移并将所做的更改放入其中。 Then you can run the new migration with rake db:migrate . 然后,您可以使用rake db:migrate运行新rake db:migrate

Whenever you run rake db:migrate,it runs all the pending migrations using the timestamp which every migration file has ..such as YYYYMMDDHHMMSS_create_products.rb. 每当您运行rake db:migrate时,它将使用每个迁移文件具有..的时间戳(例如YYYYMMDDHHMMSS_create_products.rb.运行所有待处理的迁移YYYYMMDDHHMMSS_create_products.rb.

Any file which has a migrations greater than the previously run migration timestamp will be picked up during rake db:migrate and then checked whether the changes are present in the db. rake db:migrate期间,将拾取任何具有比以前运行的迁移时间戳大的rake db:migrate ,然后检查rake db:migrate是否存在更改。 For example : For a migration file 20080906120001_add_details_to_products.rb ...if you have ran it then all the changes will be added in the db.if you edit it and then run it again then it won't be picked up as timestamp of migration should be grater then previously ran file,which is not. 例如:对于迁移文件20080906120001_add_details_to_products.rb ...如果已运行,则所有更改将添加到数据库中。如果对其进行编辑然后再次运行,则不会将其作为迁移时间戳记应该比以前运行的文件要好一些,而不是。 You can manually change the migrations by editing few numbers so that it gets picked up again without creating a new file. 您可以通过编辑一些数字来手动更改迁移,以便再次拾取它而无需创建新文件。

I would recommend create a new one as they are way to maintain and are each migration should be unique. 我建议创建一个新的,因为它们是维护方式,并且每个迁移都应该是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:如何使用rake db:migrate恢复迁移? - Ruby on Rails: How can I revert a migration with rake db:migrate? 在运行rake db:migrate时,创建迁移以向表添加列的Rails会导致错误 - Rails creating migration to add columns to table causes error when running rake db:migrate 如何自动将OSM数据集加载到rake db上的rails postgresql db中:create或migration - How can I automatically load a OSM data set into my rails postgresql db on rake db:create or migrate 添加新列时如何避免PG :: InFailedSqlTransaction? - How can I avoid PG::InFailedSqlTransaction when adding new columns? 在运行rake db:migrate VERSION = 0并重新进行迁移之后,我丢失了数据库中的所有内容 - After I ran rake db:migrate VERSION=0 and redid the migration, I lost everything in my database 我该如何使用Mongoid和活动记录进行“ rake db:migrate”操作? - How can I do “rake db:migrate” with mongoid and active record? 当我尝试执行rake db:migrate - When I try to do a rake db:migrate 当我耙db:migrate时,出现ArgumentError - When I rake db:migrate, an ArgumentError appears Ruby on Rails + devise:如何使用devis rake db:migrate创建自定义用户表? - Ruby on Rails + devise: How can i create customize users table with devis rake db:migrate? Rake db:migrate - 如何知道是否存在非运行迁移 - Rake db:migrate - how do I know if there is an unrun migration
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM