sania@sania-Swift-SF314-52:~/库$ rake db:migrate
== 20180407084831图书:迁移============================================ -- create_table(:图书) rake中止!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::StatementIn有效值:Mysql2 2::Error:表‘book’已经存在:创建表books (id bigint NOT AUTO_INCREMENT主键,title varchar(32) NOT NULL,price float,subject_id int,description text,created_at时间戳NULL) ENGINE=InnoDB ENGINE=InnoDB‘`up’`up
原因如下:
Mysql2::Error: Table 'books' already exists
/home/sania/library/db/migrate/20180407084831_books.rb:4:in `up'
Tasks: TOP => db:migrate(请参阅使用-- trace运行任务的完整跟踪)
这是20180407084831_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这是20180407072616_create_books.rb文件的内容
class CreateBooks < ActiveRecord::Migration[5.1]
def change
create_table :books do |t|
t.timestamps
end
end
end请帮我解决这个问题。我对rails很陌生。我也尝试过删除db,但是使用下面的错误进行rakes中止
拉克流产了!ActiveRecord::NoEnvironmentInSchemaError:
在架构中找不到环境数据。若要解决此问题,请运行:
bin/rails db:environment:set RAILS_ENV=development我无法理解其原因。请帮我解释一下。提前感谢
发布于 2018-04-08 06:12:17
如果需要对特定文件进行向下迁移,则运行具有版本号的命令,如下所示
rake db:migrate:down VERSION=20180407084831 #=> or rails您可以使用rails d migration命令销毁特定的迁移文件:
rails d migration MigrationName更新
看,如果您的项目现在是学习的第一步,然后删除所有项目,然后尝试从零开始(如rails new project_name -d mysql ),它将生成一个带有默认MySQL数据库适配器的项目,然后再绑定exec :迁移它将迁移一个数据库,如果您的database.yml是安全的,我的意思是用户名和密码。我想它能解决你的问题。首先,它正在发生,所以不要沮丧地继续前进。参见指南Rails Official Guide和Learn Web Development with Rails - Michael Hartl,对于新手来说,这将帮助很多人。
发布于 2018-04-08 06:16:43
https://stackoverflow.com/questions/49714492
复制相似问题