rake db:migrate error:db_development.locations'不存在:显示来自`位置的完整字段

时间:2016-07-05 14:07:11

标签: mysql ruby-on-rails ruby

的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

我安装了gem mysql2

我在mysql控制台中创建了数据库。

运行rake db:migrate并显示错误: db_development.locations'不存在:显示来自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

有什么不妥,怎么解决这个问题?

2 个答案:

答案 0 :(得分: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

现在,如果您使用此订单创建这些模型,则在doctor之后和hospital之后,当您执行迁移时,您会遇到错误。 对于解决方案,您必须先创建模型hospital,然后doctor;因为doctor的引用列为hospital

andolasoft是对的,谁在这个问题上发表评论。

答案 1 :(得分:0)

类型是mysql https://dev.mysql.com/doc/refman/5.5/en/keywords.html

中受保护的术语

请更改

$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
                'SELECT p
                FROM AppBundle:Hotel p
                WHERE p.address like :location
                ORDER BY p.address ASC'
                )->setParameter('location','%'.$request->get('location').'%' );
$hotel = $query->getResult();

t.string :type

并尝试迁移