Mysql rakedb:migrate错误:db#u development.locations';不';不存在:显示“位置”中的完整字段`
database.ymlMysql rakedb:migrate错误:db#u development.locations';不';不存在:显示“位置”中的完整字段`,mysql,ruby-on-rails,ruby,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-ge
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
我安装了gemmysql2
我在mysql控制台中创建了数据库
运行后rake db:migrate and display错误:db\u 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
class CreateLocations
问题出在哪里?如何解决这个问题 类型在mysql中是一个受保护的术语
请换
t.string :type
到
然后尝试迁移迁移文件的顺序会导致此错误。例如,假设我们有两个这样的迁移文件:
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
hospital.rb
:
class Doctor < ApplicationRecord
belongs_to :hospital
end
class Hospital < ApplicationRecord
has_many :doctors
end
一级医院
现在,如果您按照此顺序创建这些模型,在医生
和医院
之后,当您执行迁移时,您将按照要求出现错误。
对于解决方案,您必须先创建模型医院
,然后创建模型医生
;因为doctor
有一个引用列指向hospital
是的,谁在问题下发表了评论。请检查您的迁移文件。有些迁移可能涉及位置。因此,请按迁移架构id的顺序进行检查。其中一个迁移必须在创建位置之前引用位置表。什么是不正确的?只需检查其他可能指向位置的迁移。当前迁移文件是正确的。您应该检查在此迁移文件之前运行的上一个架构id(时间戳),所有迁移都是正确的,因为我是在另一台电脑上运行的。您能在这里发布您的schema.rb
文件吗?您可能希望使用带下划线的列名,而不是带驼背的列名,以避免违反Rails惯例。
class Doctor < ApplicationRecord
belongs_to :hospital
end
class Hospital < ApplicationRecord
has_many :doctors
end