【问题标题】:Rails not running migrationsRails 没有运行迁移
【发布时间】:2016-08-20 10:43:21
【问题描述】:

我遇到了一个非常奇怪的问题,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 中的其他项目可以在这台机器上运行,但这个不能。

提前感谢您的任何建议!

【问题讨论】:

  • rake db:migrate:up 需要一个 version 指定的号码,就像你的情况一样,你可以这样做 rake db:migrate:up VERSION=20160627035230

标签: ruby-on-rails ruby postgresql


【解决方案1】:

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

在您的情况下,您需要检查您机器上的 schema_search_path 是什么。也许您应该在 database.yml 文件中将其设置为“public”。可以通过SHOW search_path查询查看。

【讨论】:

  • 完全正确。旧的 Rails 代码将show search_path 的结果拆分为,,这与新的默认值不同。解决方法是将以下内容放在database.yml 的连接设置中:schema_search_path: '"$user",public'
【解决方案2】:

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

rake db:migrate VERSION=20160627035230

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-05-30
    • 2010-11-21
    • 2015-09-03
    • 2017-08-31
    相关资源
    最近更新 更多