迁移 DB Rails 时出错
Posted
技术标签:
【中文标题】迁移 DB Rails 时出错【英文标题】:Error to Migrate DB Rails 【发布时间】:2013-09-28 12:08:18 【问题描述】:我正在尝试使用此迁移,但我不断收到错误,例如这些
这是我的迁移文件
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 中止!发生错误,此迁移和所有后续迁移均已取消:
SQLite3::SQLException: 表“employees”已经存在:CREATE TABLE "employees" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar (255), "hiredate" date, "salary" float, "fulltime" boolean DEFAULT 't', "vacationdays" 整数, "cmets" 文本) /库/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
block in log' /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
log' /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:inblock in method_missing' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in
block in say_with_time' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:insay_with_time' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:617:in
method_missing' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:383:inmethod_missing' /Users/YuriAguirre/Documents/Aptana Studio 3 Workspace/company/db/migrate/20130923140626_create_employees.rb:3:in
up' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:534:inup' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:574:in
exec_migration' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:555:inblock (2 levels) in migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in
block in 迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:inwith_connection' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in
migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:inmigrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in
block 在 execute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005: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
block 在交易中' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210: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
transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:intransaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in
ddl_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:inexecute_migration_in_transaction' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in
block in 迁移' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:ineach' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:inup' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in
migrate' /Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in '任务中的`block(2个级别):TOP => db:migrate(请参阅 通过使用 --trace 运行任务来完整跟踪)
【问题讨论】:
您已收到答案:SQLite3::SQLException: table "employees" already exists
您没有提供足够的详细信息来判断创建此表的原因,可能是您手动创建的,也可能是由另一个迁移创建的,谁知道呢。
哦,现在我看到了,我是 Rails 的新手,如何重新创建或删除已经存在的表?
最好的方法是对 db 所做的所有更改都使用迁移,这样您以后就不会迷路了。我不知道您是想从头开始创建该表还是对其进行修改,后一种您应该使用add_column :table_name, :column_name, :column_type
语法
哦,明白了,但我仍然不知道如何现在删除该表并再次创建该表,哈哈,因为我不知道是否使用正确的语句创建了该表。
您在提供的迁移中得到了答案;) 只需在 self.up
部分(或 change
而不是 self.up
和 self.down
)中写入 drop_table :employees
或手动删除它从 sqlite 控制台。
【参考方案1】:
您的员工表已在您的数据库中创建。您需要修复它,然后您可以通过两种方式进行修复:1. 删除数据库并再创建一个或 2. 运行向下迁移然后向上迁移
rake db:drop
rake db:create
rake db:migrate
【讨论】:
【参考方案2】:正如我在评论中所写,您收到该错误的原因是:
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
,减少输入字母和重复;)
【讨论】:
【参考方案3】:您的迁移未完全运行。您应该回滚迁移并再次迁移。
rails db:rollback
rails db:migrate
【讨论】:
【参考方案4】:您已经运行了该迁移,但其中有错误。所以迁移进行到一半,创建的表不完整。回滚该迁移,然后再次运行迁移。
【讨论】:
以上是关于迁移 DB Rails 时出错的主要内容,如果未能解决你的问题,请参考以下文章
在 Heroku 上执行 rake db:migrate 时出错