2016-01-30 165 views
-1

我在使用Postgres時遇到了這個問題。任何人都可以解釋我這個錯誤?

rake aborted! 
StandardError: An error has occurred, this and all later migrations canceled: 

PG::UndefinedTable: ERROR: relation "retailers" does not exist 
: ALTER TABLE "stations" ADD CONSTRAINT "fk_rails_57ee36b830" 
FOREIGN KEY ("retailer_id") 
    REFERENCES "retailers" ("id") 
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change' 
ActiveRecord::StatementInvalid: PG::UndefinedTable: ERROR: relation "retailers" does not exist 
: ALTER TABLE "stations" ADD CONSTRAINT "fk_rails_57ee36b830" 
FOREIGN KEY ("retailer_id") 
    REFERENCES "retailers" ("id") 
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change' 
PG::UndefinedTable: ERROR: relation "retailers" does not exist 
/home/suyesh/Desktop/Petrohub_main/db/migrate/20160104152245_create_stations.rb:3:in `change' 
Tasks: TOP => db:migrate 
(See full trace by running task with --trace) 

這裏是我的車站和零售商模型。

零售商

class Retailer < User 
    has_many :stations 
    has_many :retailer_suppliers 
    has_many :suppliers , through: :retailer_suppliers, as: :connections 
end 

class Station < ActiveRecord::Base 
    belongs_to :retailer 
    has_many :tanks 
end 

這是我站遷移

class CreateStations < ActiveRecord::Migration 
    def change 
    create_table :stations do |t| 
     t.string :brand 
     t.string :business_name 
     t.string :tax_id 
     t.string :phone_number 
     t.string :contact_person 
     t.string :cell_number 
     t.string :address1 
     t.string :address2 
     t.string :city 
     t.string :state 
     t.string :zip 
     t.string :station_reg_number 
     t.references :retailer, index: true, foreign_key: true 

     t.timestamps null: false 
    end 
    end 
end 

我沒有零售商遷移,因爲它從用戶繼承。這裏是我的用戶遷移

用戶

class DeviseCreateUsers < ActiveRecord::Migration 
    def change 
    create_table(:users) do |t| 
     ## Database authenticatable 
     t.string :email,    null: false, default: "" 
     t.string :encrypted_password, null: false, default: "" 

     ## Recoverable 
     t.string :reset_password_token 
     t.datetime :reset_password_sent_at 

     ## Rememberable 
     t.datetime :remember_created_at 

     ## Trackable 
     t.integer :sign_in_count, default: 0, null: false 
     t.datetime :current_sign_in_at 
     t.datetime :last_sign_in_at 
     t.string :current_sign_in_ip 
     t.string :last_sign_in_ip 

     ## Confirmable 
     # t.string :confirmation_token 
     # t.datetime :confirmed_at 
     # t.datetime :confirmation_sent_at 
     # t.string :unconfirmed_email # Only if using reconfirmable 

     ## Lockable 
     # t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts 
     # t.string :unlock_token # Only if unlock strategy is :email or :both 
     # t.datetime :locked_at 


     t.timestamps null: false 
    end 

    add_index :users, :email,    unique: true 
    add_index :users, :reset_password_token, unique: true 
    # add_index :users, :confirmation_token, unique: true 
    # add_index :users, :unlock_token,   unique: true 
    end 
end 

添加類型到用戶

class AddTypeToUsers < ActiveRecord::Migration 
    def change 
    add_column :users, :type, :string 
    end 
end 

添加額外的屬性,用戶

class AddExtraToUsers < ActiveRecord::Migration 
    def change 
    add_column :users, :first_name, :string 
    add_column :users, :last_name, :string 
    add_column :users, :phone_number, :string 
    add_column :users, :cell_number, :string 
    add_column :users, :tax_id, :string 
    add_column :users, :business_name, :string 
    add_column :users, :address1, :string 
    add_column :users, :address2, :string 
    add_column :users, :city, :string 
    add_column :users, :state, :string 
    add_column :users, :zip_code, :string 
    add_column :users, :years_in_business, :string 
    end 
end 

並添加帳號用戶

class AddAccountNumberToUsers < ActiveRecord::Migration 
    def change 
    add_column :users, :account_number, :string 
    add_index :users, :account_number 
    end 
end 

它使用沒有錯誤,當我使用Sqlite3但我在生產heroku使用postgres中的錯誤。所以我決定在開發中使用postgres,我看到了上面的錯誤,我無法理解。謝謝你在前進

回答

1
PG::UndefinedTable: ERROR: relation "retailers" does not exist 

此錯誤只是意味着retailers表不存在於數據庫中,當您試圖在另一個地方引用此。您只需確保在創建retailers表之後,在嘗試在某些遷移中使用/引用它之前。

+0

零售商從用戶繼承。所以我想零售商表是不需要的。它只是用戶表。所以我應該將代碼更改爲has_many用戶等? – suyesh

+1

你仍然可以使用'零售商',但指定類名稱爲:'用戶'就像這樣:'belongs_to:retailer,class_name:'User'' and'has_many:retailers,class_name:'User'' –

+0

Ahhh讓我試試。 – suyesh

0

錯誤是由該行未來的遷移:

t.references :retailer, index: true, foreign_key: true 

外鍵選項意味着軌試圖創建於不存在的零售商表retailer_id上站表和ID之間的外鍵。

雖然遷移經常與模型同時創建,但它們並沒有真正連接 - 遷移並不知道零售商是STI模型。

至於我可以告訴你需要與add_foreign_key單獨刪除從通話外鍵選項references並添加外:

add_foreign_key :stations, :users, column: "retailer_id" 

你可能沒有在發展遇到這種因爲早期版本的sqlite3不支持外鍵,當前版本要求將其激活(請參閱docs):除非被激活,否則只是忽略外鍵約束(只要它們在語法上是正確的)

相關問題