Rails未运行迁移

时间:2016-08-20 10:43:22

标签: ruby-on-rails ruby postgresql

我遇到了一个非常奇怪的问题,即rails不会在我的机器上执行迁移。我在OS X 10.11.6上使用Rails 4.2.5,Ruby 2.3.1p112和Postgres 9.5.3。

数据库在Heroku和另一台具有相同版本的Mac上正确创建/迁移和运行。

如果我从Heroku或其他计算机导入数据库,则在运行rails s时会出现挂起的迁移错误,即使我使用的是相同的源版本。

运行db:schema告诉我:

You have 23 pending migrations:
  20160627035230 DeviseCreateUsers
  20160627035442 CreatePages
  20160627055031 AddHeroToPages
  20160627061237 AddAdministratorToUser
  20160627061943 AddNameToUser
  20160628042547 CreateSchools
  20160628042702 CreateLicenses
  20160628042841 JoinUsersAndSchools
  20160628043323 AddJoinCodeToSchools
  20160630012028 AddExpiryToLicenses
  20160630024743 CreatePurchases
  20160630043846 AddOmniauthToUsers
  20160704003626 CreateMyIndustries
  20160704010403 CreateMyCareers
  20160704013913 CreateMyQuestions
  20160704074916 AlterMyQuestionsDrop
  20160704075947 AlterMyQuestionsChange
  20160705071702 CreateUserAttributes
  20160710235339 CreateAccessabilities
  20160711000000 AddToAccessabilities
  20160711044815 CreateMedia
  20160711045327 AddLocaleToMedium
  20160814010549 CreateInvoices
Run `rake db:migrate` to update your database then try again.

运行迁移然后告诉我:

** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environment
** Invoke db:load_config (first_time)
** Execute db:load_config
** Execute db:migrate
  ActiveRecord::SchemaMigration Load (0.3ms)  SELECT "schema_migrations".* FROM "schema_migrations"
** Invoke db:_dump (first_time)
** Execute db:_dump
** Invoke db:schema:dump (first_time)
** Invoke environment 
** Invoke db:load_config 
** Execute db:schema:dump
  ActiveRecord::SchemaMigration Load (0.2ms)  SELECT "schema_migrations".* FROM "schema_migrations"

在psql中列出表只显示schema_migrations:

20:09 $ rails db
psql (9.5.3)
Type "help" for help.

mytalents_development=# \dt
                  List of relations
 Schema |       Name        | Type  |     Owner      
--------+-------------------+-------+----------------
 public | schema_migrations | table | aidancornelius
(1 row)

mytalents_development=# 

db:migrate:up

似乎也出现了奇怪的错误
20:11 $ rake db:migrate:up
rake aborted!
ActiveRecord::UnknownMigrationVersionError: 

No migration with version number 0

/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/migration.rb:939:in `run'
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/migration.rb:834:in `run'
/usr/local/lib/ruby/gems/2.3.0/gems/activerecord-4.2.5/lib/active_record/railties/databases.rake:82:in `block (3 levels) in <top (required)>'
/usr/local/lib/ruby/gems/2.3.0/gems/rake-11.2.2/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate:up
(See full trace by running task with --trace)

有没有人对这里可能发生的事情有任何见解? Rails 4和Rails 5中的其他项目可以在这台机器上运行,但是这个项目不会。

提前感谢任何建议!

2 个答案:

答案 0 :(得分:2)

我敢打赌这是Postgres Schemas的一个问题。我有类似的问题,但在我的情况下,迁移工作在我的本地机器上,而不是在Heroku上。它是由schema_search_path的不同设置引起的。在我的本地计算机上,它是'"$user",public',但在Heroku上'"$user", public'",这个额外的空间打破了一切。

在您的情况下,您需要检查机器上schema_search_path的内容。也许你应该把它设置为&#34; public&#34;在database.yml文件中。您可以使用SHOW search_path查询进行检查。

答案 1 :(得分:1)

rails迁移文件的序列(和迁移过程)是最重要的部分,因此您可以尝试使用以下命令迁移第一个迁移文件:

rake db:migrate VERSION=20160627035230

然后只使用db:migratedb:migrate Version=20160814010549