2014-03-13 33 views
0

我有一個新的Rails應用程序將從現有數據庫(由另一個ruby應用程序創建)消耗。 要做到這一點,我創建了一個已經存在的數據庫表的模型,但現在軌給我,我要運行Rake db:針對現有數據庫遷移

rake db:migration 

但是,如果我嘗試這樣做,我得到一個錯誤,因爲錯誤信息表已經存在。

有沒有辦法執行遷移並忽略現有表?該表是正確的,應該在那裏,並填充了用於其他應用程序的數據。我想讓這個應用程序消費這些信息。

感謝

編輯: 的DB設置就可以了,因爲我是能夠執行DB:之前遷移。我創建使用

rails g model fundo 

(豐杜的是模型的名稱和fundoS是表的名稱) 該模型沒有財產還,但該表中的列

編輯2型號: 這些都是輸出,如果我運行--trace

$ rake db:schema:dump --trace

** Invoke db:schema:dump (first_time)

** Invoke environment (first_time)

** Execute environment

** Invoke db:load_config (first_time)

** Execute db:load_config ** Execute db:schema:dump

$ rake db:migrate --trace

** Invoke db:migrate (first_time)

** Invoke environment (first_time)

** Execute environment

** Invoke db:load_config (first_time)

** Execute db:load_config

** Execute db:migrate == CreateFundos: migrating ===================================================

-- create_table(:fundos) rake aborted! An error has occurred, this and all later migrations canceled: PG::DuplicateTable: ERROR: relation "fundos" already exists CREATE TABLE "fundos" ("id" serial primary key, "created_at" timestamp, "updated_at" timestamp)

看來,軌道試圖重新創建表。但我只想讓他們同步,因爲桌子已經在那裏了!

+0

您能確認您的數據庫配置是否正確,並且您現有的表命名是否符合新創建模型的Rails標準? – vee

+0

@vee更新了問題;) – JSBach

回答

1

如果使用rails g model爲現有表創建模型,只需刪除所創建的遷移文件。

表格架構將正確轉儲到schema.rb中,因此即使沒有遷移文件,也可始終從其他機器上從頭創建rake db:setup。可以使用rake db:schema:dump來手動更新schema.rb。

1

是否有很多想要執行的遷移文件?如果不是太多,則可以通過

rake db:migrate:redo VERSION=version 

進行遷移的特定版本如果遷移文件創建表並不太多,也許你可以通過添加編輯遷移文件:

if ActiveRecord::Base.connection.table_exists?(table_name) 

之前創建桌子。

在您的本地環境中,也許您可​​以刪除不必要的文件。

1

cd db/migrate/

ls | cut -d'_'f1 |而讀線; do bundle exec rake db:migrate:up VERSION = $ line;做

運行所有遷移文件

0

-- create_table(:fundos) rake aborted! An error has occurred, this and all later >migrations canceled: PG::DuplicateTable: ERROR: relation "fundos" already exists >CREATE TABLE "fundos" ("id" serial primary key, "created_at" timestamp, >"updated_at" timestamp)

我會做的就是去DB /遷移,並轉到遷移文件,其中CREATE_TABLE(:fundos)發生。評論線。再試一次,如果它再次拋出錯誤,請檢查錯誤並找到有問題的代碼。然後評論它,並繼續這樣做,直到它通過。一旦它通過,不評論一切。