簡體   English   中英

Rails 3 - 突然發生的遷移錯誤

[英]Rails 3 - suddenly pending migrations error

我突然遇到一個奇怪的錯誤。 當我嘗試運行rake spec我收到:

You have 2 pending migrations:
  20130405105004 CreateReports
  20130405113839 AddDocumentToReports

我不知道原因(我過去運行遷移所以我在數據庫和schema.rb中有數據)。

這是rake spec --trace

** Invoke spec (first_time)
** Invoke test:prepare (first_time)
** Invoke db:test:prepare (first_time)
** Invoke db:abort_if_pending_migrations (first_time)
** Invoke environment (first_time)
** Execute environment
** Execute db:abort_if_pending_migrations
You have 2 pending migrations:
  20130405105004 CreateReports
  20130405113839 AddDocumentToReports
Run "rake db:migrate" to update your database then try again.

這是rake db:migrate:status

   ...
   up     20121210112419  Create simple captcha data
   up     20130214110545  Add weeknum to alerts
  down    20130405105004  Create reports
  down    20130405113839  Add document to reports
   up     20121018133601  *** NO FILE ***
   up     20121018163051  *** NO FILE ***
   up     20121024124111  *** NO FILE ***

這里是rake db:migrate

==  CreateReports: migrating ==================================================
-- create_table(:reports)
rake aborted!
An error has occurred, all later migrations canceled:

Mysql2::Error: Table 'reports' already exists: CREATE TABLE `reports` (`id` int(11) DEFAULT NULL auto_increment PRIMARY KEY, `user_id` int(11), `ready_status` tinyint(1) DEFAULT 0, `created_at` datetime, `updated_at` datetime) ENGINE=InnoDB

Tasks: TOP => db:migrate

我該如何解決這個問題?

如果這是在開發環境中並且您在數據庫中沒有重要數據,則運行

bundle exec rake db:migrate:reset

這將重建架構,但代價是核對所有數據。

通過使用Recks答案和代碼重建模式,我能夠完成所有工作

bundle exec rake db:migrate:reset

在我的postgres數據庫中創建了一個schema_migrations表,您可以通過刪除該表來重置遷移狀態( drop table schema_migrations; )。

我有* No File *的類似問題。 我做了一個rake db:migrate:status並發現rake正在尋找我在migrate文件夾下的另一個文件夾中保存的一些舊遷移。 所以,我刪除了舊的遷移和schema.rb。 然后我做了rake db:drop,rake db:create和rake db:migrate。 這解決了這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM