1
votes

database.yml

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

I installed gem mysql2

I created database in mysql console.

After run rake db:migrate and display error: db_development.locations' doesn't exist: SHOW FULL FIELDS FROM locations

one migration from migrations:

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

What wrong and how solve this problem?

2
Please check your migration files. Some of the migrations might be referring to location. So check in order of migration schema id. One of the migrations must be refering to the locations table before the create locations table migrationAndolasoft
i aded example of migration in question. what is not correct?Dmitrij
Just check other migrations which might be refering to locations. The current migration file is correct. You should check the previous schema id (timestamp) which are running before this migration fileAndolasoft
all migrations are correct, becose i runed on another pc.Dmitrij
can you post your schema.rb file here?Pramod

2 Answers

1
votes

The order of migration files leads this error. For example lets assume we've two migration files like that:

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

and this:

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

So, we have also model files:

doctor.rb:

class Doctor < ApplicationRecord
  belongs_to :hospital
end

hospital.rb:

class Hospital < ApplicationRecord
  has_many :doctors
end

Now, if you create these models with this order, once doctor and after hospital, when you do migration you will have an error as you asked. For the solution, you have to create models first hospital, then doctor; because doctor has a referenced column to hospital.

andolasoft was right, who makes comment under the question.

0
votes

Type is a protected term in mysql https://dev.mysql.com/doc/refman/5.5/en/keywords.html

please change

t.string :type

to

t.string :locationType

and try migrating