我试图使用此迁移,但我不断收到错误,例如
这是我的迁移文件
class CreateEmployees < ActiveRecord::Migration
def self.up
create_table :employees do |t|
t.string :name
t.date :hiredate
t.float :salary
t.boolean :fulltime , :default => true
t.integer :vacationdays
t.text :comments
end
end
def self.down
drop_table :employees
end
end
当我尝试执行rake db:migrate时出现此错误:
== CreateEmployees:迁移========================================== ====== - create_table(:employees)rake aborted!发生错误,此操作和所有后续迁移都已取消:
SQLite3 :: SQLException:表“employees”已经存在:CREATE TABLE “雇员”(“id”INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,“name” varchar(255),“hiredate”date,“salary”float,“fulltime”boolean DEFAULT't',“休假日”整数,“评论”文字) /Library/Ruby/Gems/2.0.0/gems/sql ite3-1.3.8 / lib / sqlite3 / database.rb:91:in
initialize' /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in
new' /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:inprepare' /Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in
执行” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:inblock in execute' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:in
阻止日志' /Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:ininstrument' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:in
登录” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:inexecute' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in
create_table' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:625:in {_ 1}}阻止在say_with_time' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:inblock in method_missing' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in
的method_missing” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:383:insay_with_time' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:617:in
向上” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:534:inmethod_missing' /Users/YuriAguirre/Documents/Aptana Studio 3 Workspace/company/db/migrate/20130923140626_create_employees.rb:3:in
exec_migration” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:555:inup' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:574:in
阻止迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:inblock (2 levels) in migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in
迁移” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:in execute_migration_in_transaction中的with_connection' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in
阻止 /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in 交易migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in
阻止 /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:inblock in ddl_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
交易” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:inwithin_new_transact ion' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
ddl_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:intransaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in
阻止迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:inexecute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in
迁移” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:ineach' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
迁移” /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in '任务:TOP =&gt;中的'阻止(2级) db:migrate(参见 使用--trace运行任务完全跟踪
答案 0 :(得分:2)
您的员工表已在数据库中创建。您需要修复它,然后您可以通过两种方式执行此操作1.删除数据库并再创建一个或2.运行向下迁移然后向上迁移
rake db:drop
rake db:create
rake db:migrate
答案 1 :(得分:1)
正如我在评论中写的那样,您收到的错误是因为:
SQLite3::SQLException: table "employees" already exists
这意味着您已经在数据库中拥有该列。您可以通过以下几种方式解决这个问题:
将迁移更改为修改而不是创建:
class CreateEmployees < ActiveRecord::Migration
def self.up
add_column :employees, :name, :string
(...)
end
end
def self.down
remove_column :employees, :name
end
end
添加另一个将删除整个表的迁移(显然,那个应该使用更早的时间戳而不是修改一个,因此表将首先被删除然后再次创建):
class RemoveEmployees < ActiveRecord::Migration
def change
drop_table :employees
end
end
从控制台手动删除表,但这不是一个很好的选择,因为您应该学会使用迁移来更改数据库,当然这是最快的(如果你知道如何使用控制台,如果这只是一些学习项目,那将不会造成伤害,但是,你应该尽可能地学习好习惯。
您可能还想使用较新的语法(AFAIK)def change
而不是self.up
和self.down
,减少输入的字母数量,减少重复次数;)
答案 2 :(得分:0)
您已经运行了该迁移,其中包含错误。因此迁移中途运行,不完全创建表。回滚该迁移,然后再次运行迁移。
答案 3 :(得分:0)
您的迁移未完全运行。您应回滚迁移并再次迁移。
rails db:rollback
rails db:migrate