Ruby-on-rails Rails:我更新迁移文件然后运行 ​​db:migrate,但我的架构没有更新

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1416600/
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 21:45:04  来源:igfitidea点击:

Rails: I update migration file then run db:migrate, but my schema isn't updating

ruby-on-railsdbmigrateschema.rb

提问by Evolve

I'm trying to add an extra field to one of my tables.

我正在尝试向我的一个表中添加一个额外的字段。

I've added the field in the migration file (under db\migrate), then ran 'rake db:migrate' which ran without troubles. My text editor even told me my schema.db file has been updated and needs to refresh.

我已经在迁移文件中添加了该字段(在 db\migrate 下),然后运行了 'rake db:migrate',它运行起来没有任何问题。我的文本编辑器甚至告诉我我的 schema.db 文件已经更新,需要刷新。

The schema file does not contain my new field and any attempts to reference the field from my views fail miserably.

架构文件不包含我的新字段,任何从我的视图中引用该字段的尝试都失败了。

How do I do this? It is possible to update a table with an extra field via rails without having to totally drop and recreate the database again?

我该怎么做呢?是否可以通过 rails 更新带有额外字段的表,而不必完全删除并再次重新创建数据库?

回答by utapyngo

http://guides.rubyonrails.org/migrations.html#changing-existing-migrations

http://guides.rubyonrails.org/migrations.html#changed-existing-migrations

Occasionally you will make a mistake when writing a migration. If you have already run the migration then you cannot just edit the migration and run the migration again: Rails thinks it has already run the migration and so will do nothing when you run rake db:migrate. You must rollback the migration (for example with rake db:rollback), edit your migration and then run rake db:migrateto run the corrected version.

有时您会在编写迁移时出错。如果您已经运行了迁移,那么您不能仅仅编辑迁移并再次运行迁移:Rails 认为它​​已经运行了迁移,因此在您运行 rake db:migrate 时不会做任何事情。您必须回滚迁移(例如使用rake db:rollback),编辑迁移,然后运行rake db:migrate以运行更正后的版本。

回答by jluebbert

You should always create a new migration file when adding/changing something in the database. This is the purpose of migrations. A migration file should have the ability to make the new change and undo the change. This way if something goes wrong or you changed your mind you can easily roll back to a previous migration.

在数据库中添加/更改某些内容时,您应该始终创建一个新的迁移文件。这就是迁移的目的。迁移文件应该能够进行新的更改和撤消更改。这样,如果出现问题或改变主意,您可以轻松回滚到之前的迁移。

The following link's sections labeled 'Anatomy of a Migration' and 'Writing a Migration' might be of help to you.

以下链接中标有“迁移剖析”和“编写迁移”的部分可能对您有所帮助。

http://guides.rubyonrails.org/migrations.html

http://guides.rubyonrails.org/migrations.html

回答by Evolve

Solved my own Question..

解决了我自己的问题..

Basically rather than edit the original migration files generated when you run scaffolding you create a new migration file just for what you want to acheive:

基本上,不是编辑在运行脚手架时生成的原始迁移文件,而是为您想要实现的目标创建一个新的迁移文件:

http://guides.rubyonrails.org/migrations.html#creating-a-standalone-migration

http://guides.rubyonrails.org/migrations.html#creating-a-standalone-migration

回答by sarmahdi

I did the same thing, I wanted to change a field name and instead of this:

我做了同样的事情,我想改变一个字段名称而不是这个:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.string :commenter
      t.text :body

      # this line adds an integer column called `article_id`.
      t.references :article, index: true

      t.timestamps
    end
  end
end

I changed

我变了

  t.text :body

to

t.text :comment_body

I tried doing rake

我试着做耙子

db:migrate

nothing happened as in it went to the command prompt again without any output..., i looked at stack overflow and thisguided me to do rake

什么也没发生,因为它在没有任何输出的情况下再次进入命令提示符......,我看着堆栈溢出,引导我做 rake

db:migrate:redo

with out put

输出

== 20141129044056 CreateComments: reverting ===================================
-- drop_table(:comments)
   -> 0.0000s
== 20141129044056 CreateComments: reverted (0.0886s) ==========================

== 20141129044056 CreateComments: migrating ===================================
-- create_table(:comments)
   -> 0.0040s
== 20141129044056 CreateComments: migrated (0.0040s) ==========================

and then I loaded my page/controller with commenter_body instead of body and it loaded perfectly.

然后我用 commenter_body 而不是 body 加载了我的页面/控制器,它完美地加载了。

I think this is also a solution to the same. I dont know if there is any issue in the underneath workings in the model/DB( i am still very new to RoR,my third day actually...)

我认为这也是一个解决方案。我不知道模型/数据库的底层工作是否有任何问题(我对 RoR 还是很陌生,实际上我的第三天......)

回答by lfender6445

I was able to regenerate my schema with latter migrations by running rake db:schema:dump

我能够通过运行重新生成我的架构与后期迁移 rake db:schema:dump

回答by JRL

Don't know if this applies, but it's worth a shot. Straight from "Agile Development with Rails, 3rd edition":

不知道这是否适用,但值得一试。直接来自“使用 Rails 进行敏捷开发,第 3 版”:

Sometimes this schema_migrations table can cause you problems. For example, if you create the migration source file and run db:migratebefore you add any schema-defining statements to the file, the database will think it has been updated, and the schema info table will contain the new version number.
If you then edit that existing migration file and run db:migrateagain, Rails won't know to apply your new changes. In these circumstances, it's often easiest to drop the database, re-create it, and rerun your migration(s).

有时这个 schema_migrations 表会给你带来问题。例如,如果在向文件db:migrate添加任何架构定义语句之前创建迁移源文件并运行,数据库将认为它已更新,并且架构信息表将包含新版本号。
如果您随后编辑该现有迁移文件并db:migrate再次运行,Rails 将不知道应用您的新更改。在这些情况下,删除数据库、重新创建它并重新运行迁移通常是最简单的。

回答by Kranthi

Once u do rake db:migrate then again u cannot add column to that file.You have to generate a new migration file by using rails g migration add_columnname_to_tablename for adding that particular column and do rake db:migrate.That's it !!!

一旦您执行 rake db:migrate 操作,您就无法将列添加到该文件中。您必须使用 rails g migration add_columnname_to_tablename 生成一个新的迁移文件来添加该特定列,然后执行 rake db:migrate 操作。就是这样!!!