== 20180407084831书籍:迁移========================================= === - create_table(:books) 耙子流产了! StandardError:发生错误,所有以后的迁移都被取消:
Mysql2::Error: Table 'books' already exists: CREATE TABLE `books` (`id` bigint NOT NULL AUTO_INCREMENT PRIMARY KEY, `title` varchar(32) NOT NULL, `price` float, `subject_id` int, `description` text, `created_at` timestamp NULL) ENGINE=InnoDB /home/sania/library/db/migrate/20180407084831_books.rb:4:in `up'
引起:
ActiveRecord :: StatementInvalid:Mysql2 :: Error:Table' books'已存在:CREATE TABLE books
(id
bigint NOT NULL AUTO_INCREMENT PRIMARY KEY,title
varchar(32)NOT NULL,price
float,subject_id
int,{ {1}} text,description
timestamp NULL)ENGINE = InnoDB
/home/sania/library/db/migrate/20180407084831_books.rb:4:in`up up'
引起:
created_at
(通过使用--trace运行任务查看完整跟踪)
这是20180407084831_books.rb文件的内容
Mysql2::Error: Table 'books' already exists
/home/sania/library/db/migrate/20180407084831_books.rb:4:in `up'
Tasks: TOP => db:migrate
这是20180407072616_create_books.rb文件的内容
class Books < ActiveRecord::Migration[5.1]
def self.up
create_table :books do |t|
t.column :title, :string, :limit => 32, :null => false
t.column :price, :float
t.column :subject_id, :integer
t.column :description, :text
t.column :created_at, :timestamp
end
end
def self.down
drop_table :books
end
end
请帮我解决这个问题。我是铁杆新手。我也试过删除数据库但是因为以下错误而中止了
耙子流产了! ActiveRecord :: NoEnvironmentInSchemaError:架构中找不到环境数据。要解决此问题,请运行:
class CreateBooks < ActiveRecord::Migration[5.1]
def change
create_table :books do |t|
t.timestamps
end
end
end
我无法理解它的原因。请帮我解释一下。提前致谢
答案 0 :(得分:0)
如果您需要针对特定文件进行向下迁移,则运行具有此版本号的命令
rake db:migrate:down VERSION=20180407084831 #=> or rails
您可以使用rails d migration
命令销毁特定的迁移文件:
rails d migration MigrationName
<强>更新强>
看,如果您的项目现在是学习的第一步,那么删除所有项目然后从头开始尝试rails new project_name -d mysql
它将生成一个带有默认MySQL数据库适配器的项目然后cd project_name然后捆绑exec rake db:迁移它如果您database.yml
没问题,我会迁移数据库,我的意思是用户名&amp;密码。我认为它会解决你的问题。第一次发生,所以不要沮丧继续前进。请参阅指南Rails Official Guide
和Learn Web Development with Rails - Michael Hartl
,这将有助于新手
答案 1 :(得分:0)