2016-07-05 97 views
1

的database.yml耙分貝:遷移錯誤:db_development.locations'不存在:SHOW FULL FIELDS FROM`locations`

development: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: false 
    database: db_development 
    username: root 
    password: "123" 
    socket: /var/run/mysqld/mysqld.sock 

# Warning: The database defined as "test" will be erased and 
# re-generated from your development database when you run "rake". 
# Do not set this db to the same as development or production. 
test: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: false 
    database: db_test 
    pool: 5 
    username: root 
    password: "123" 
    socket: /var/run/mysqld/mysqld.sock 

production: 
    adapter: mysql2 
    encoding: utf8 
    reconnect: false 
    database: db_production 
    pool: 5 
    username: root 
    password: "123" 
    socket: /var/run/mysqld/mysqld.sock 

我安裝了寶石mysql2

我在MySQL控制檯創建的數據庫。

後運行耙分貝:從locations

一個遷移從遷移SHOW FULL FIELDS:遷移並顯示錯誤:db_development.locations'不存在

class CreateLocations < ActiveRecord::Migration 
    def self.up 
    create_table :locations do |t| 
     t.string :name 
     t.string :type 
     t.integer :parent_id 
     t.integer :position 

     t.timestamps 
    end 
    end 

    def self.down 
    drop_table :locations 
    end 
end 

什麼錯誤,以及如何解決這個問題?

+0

請檢查您的遷移文件。一些遷移可能指的是位置。因此,請按遷移架構ID的順序進行檢查。其中一項遷移必須是在創建位置表格遷移 – Andolasoft

+0

之前提及的遷移示例。什麼是不正確的? – Dmitrij

+0

只需檢查可能涉及位置的其他遷移。當前的遷移文件是正確的。您應該檢查在此遷移文件 – Andolasoft

回答

1

的遷移文件的順序導致這個錯誤。例如讓我們假設我們已經兩種遷移文件那樣:

class CreateDoctors < ActiveRecord::Migration[5.1] 
    def change 
    create_table :doctors do |t| 
     t.string :name 
     t.string :degree 
     t.references :hospital, foreign_key: true 
    end 
    end 
end 

這:

class CreateHospitals < ActiveRecord::Migration[5.1] 
    def change 
    create_table :hospitals do |t| 
     t.string :name 
     t.string :city 
    end 
    end 
end 

所以,我們也有模型文件:

doctor.rb

class Doctor < ApplicationRecord 
    belongs_to :hospital 
end 

hospital.rb

class Hospital < ApplicationRecord 
    has_many :doctors 
end 

現在,如果你創建這些模型這樣的順序,一旦doctorhospital後,當你做你的遷移會問你有一個錯誤。 對於解決方案,您必須先創建模型hospital,然後doctor;因爲doctor的參考列爲hospital

andolasoft是對的,誰在該問題下發表評論。