Ruby-on-rails Rails 迁移不会改变 schema.rb

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

Rails migration does not change schema.rb

ruby-on-railsruby-on-rails-4database-migrationrails-migrations

提问by Don P

I have a rails migration that is not being applied to my schema.rb. The migration should create a table:

我有一个未应用于我的 schema.rb 的 rails 迁移。迁移应该创建一个表:

class CreateUserGraphs < ActiveRecord::Migration
  def change
    create_table :user_graphs do |t|
      t.string :name
      t.string :content
      t.integer :user_id
      t.string :type_id
      t.integer :upload_id

      t.timestamps
    end

    add_index :user_graphs, [:user_id, :created_at]
  end
end

I did db:reset. Then I tried rake db:migrate:up VERSION=123123123(this is the migration #). I am in my "dev" environment.

我做了 db:reset。然后我尝试 rake db:migrate:up VERSION=123123123(这是迁移#)。我在我的“开发”环境中。

Why is the migration not affecting schema.rb?

为什么迁移不会影响 schema.rb?

回答by Vucko

From the documentation:

文档

The rake db:resettask will drop the database, recreate it and load the current schema into it.

rake db:reset任务将删除数据库,重新创建它并将当前模式加载到其中。

This is not the same as running all the migrations. It will only use the contents of the current schema.rb file. If a migration can't be rolled back, 'rake db:reset' may not help you. To find out more about dumping the schema see 'schema dumping and you.'

这与运行所有迁移不同。它只会使用当前 schema.rb 文件的内容。如果迁移无法回滚,'rake db:reset' 可能无济于事。要了解有关转储模式的更多信息,请参阅“模式转储和您”。

So rake db:reset=> db:drop db:create db:schema:load db:seed

所以rake db:reset=>db:drop db:create db:schema:load db:seed

To run all the migrations, use : rake db:drop db:create db:migrate

要运行所有迁移,请使用: rake db:drop db:create db:migrate

Or db:migrate:reset=> rake db:drop db:create db:migrate

db:migrate:reset=>rake db:drop db:create db:migrate

Reference

参考

回答by Jussi Hirvi

I had the same issue. I am working in development environment (with Passenger and Apache). Production and development environments use the same database.

我遇到过同样的问题。我在开发环境中工作(使用Passenger 和Apache)。生产和开发环境使用相同的数据库。

When I run rake db:migrate, the db was changed, but the schema was not updated. Then I run rake db:migrate RAILS_ENV=development, and now the schema was updated.

当我运行时rake db:migrate,数据库已更改,但架构未更新。然后我运行rake db:migrate RAILS_ENV=development,现在架构已更新。

Seems that rails/rake are confused about my environment. Passenger sets a development environment for this site, but rake aboutsays "Environment production".

似乎 rails/rake 对我的环境感到困惑。乘客为这个站点设置了一个开发环境,但是rake about说“环境生产”。

回答by Raj

First you try to down you migration

首先,您尝试降低迁移

rake db:migrate:down VERSION=20180605141404 # "VERSION=20180605141404 your migration version"

rake db:migrate:down VERSION=20180605141404 # "VERSION=20180605141404 你的迁移版本"

And again up your migration

再次进行迁移

rake db:migrate:up VERSION=20180605141404 #"VERSION=20180605141404 your migration version"

rake db:migrate:up VERSION=20180605141404 #"VERSION=20180605141404 你的迁移版本"

回答by Mia Genovese

I had the same issue... it turns out it was because I edited the name of my migration file to look neater. Make sure you don't delete the time stamp in the migration file title like I did.

我遇到了同样的问题……原来是因为我编辑了迁移文件的名称以使其看起来更整洁。确保您没有像我一样删除迁移文件标题中的时间戳。

I deleted the migration file, the model, the controller, and associated tests and re-generated the controller and model which fixed the problem.

我删除了迁移文件、模型、控制器和相关测试,并重新生成了解决问题的控制器和模型。

回答by sevenseacat

'Versions' of migrations are done via timestamps. Rails checks which migrations it needs to run by comparing the timestamp of the last run migration and seeing if there are any newer.

迁移的“版本”是通过时间戳完成的。Rails 通过比较上次运行迁移的时间戳并查看是否有更新来检查它需要运行哪些迁移。

If the version of your new migration is 123123123, it will not be run as that number is not greater than the current timestamp (eg. 20131209170300).

如果您的新迁移版本是123123123,它将不会运行,因为该数字不大于当前时间戳(例如20131209170300)。

回答by Pavel Murnikov

Found a way to obtain an error description. ran rake db:migrate:reset and received

找到了获取错误描述的方法。运行 rake db:migrate:reset 并收到

`SQLite3::SQLException: Cannot add a NOT NULL column with default value NULL: ALTER TABLE "rooms" ADD "priority" integer NOT NULL/usr/local/rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in `initialize`'