我尝试" rake db:rollback STEP = 6"或者更多,但当我运行" rake db:migrate"它只显示正在执行的最后一个迁移文件:
== 20150508040222 CreateStructure:迁移================================== == 20150508040222 CreateStructure:已迁移(0.0000s)=========================
当我尝试使用" rails console"检查上一步中是否已创建新字段时和"放置User.new()。inspect"它显示那些字段仍然缺失。
如何回滚以便执行我编辑过的那些交易文件?
[编辑]
当我尝试使用" rake db:migrate VERSION = 20150328013052"回滚时,它在上次第二次迁移时失败,因为它无法删除不存在的字段。我认为我已经修改了迁移以在迁移已经执行后添加这些字段,因此这些字段不存在。我该怎么办?我尝试修改schema.rb中的时间戳并运行migrate,但看起来并不是跟踪当前版本的内容。
class AddLoginToUsers < ActiveRecord::Migration
def change
add_column :name, :password, :string
end
end
== 20150508040222 CreateStructure:还原================================== == 20150508040222 CreateStructure:reverted(0.0273s)=========================
== 20150506210153 AddLoginToUsers:还原================================== - remove_column(:name,:password,:string)rake aborted! StandardError:发生错误,此错误以及稍后的所有迁移 取消:
无法找到表格 &#39; name&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:517:in
table_structure' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:431:in
primary_key&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:538:incopy_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:533:in
move_table&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:526:inblock in alter_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
transaction&#39; 在/ {/ 1 /} / { /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in {_ 1}}阻止在say_with_time&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:inalter_table' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:467:in
的method_missing&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:497:inblock in method_missing' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in
各&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:496:insay_with_time' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in
exec_migration&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:inblock in revert' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:496:in
阻止迁移&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:inrevert' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:604:in
迁移&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:765:in execute_migration_in_transaction&#39;中的block (2 levels) in migrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in
阻止 /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in 交易中with_connection' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:588:in
阻止&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/transaction.rb:188:inmigrate' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:995:in
交易&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/transactions.rb:220:inblock in ddl_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in
ddl_transaction&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:994:inwithin_new_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in
阻止迁移&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:intransaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:1041:in
迁移&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:827:inexecute_migration_in_transaction' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:956:in
迁移&#39; /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/tasks/database_tasks.rb:137:ineach' /var/lib/gems/1.9.1/gems/activerecord-4.2.0/lib/active_record/migration.rb:952:in
阻止(2级)&#39;任务:TOP =&gt; db:migrate(参见 使用--trace运行任务完全跟踪
[编辑]
好的,我知道,我只需要删除迁移以反转它们,然后重写它们。问题解决了。
答案 0 :(得分:0)
我会通过打开schema.rb文件(位于db / schema.rb中)找到Schema的当前状态,第一行看起来像:
ActiveRecord::Schema.define(version: 20150507210038) do
将该版本号与db / migrate文件夹中的迁移相匹配。也许您已经进行了适当的迁移。要恢复迁移,我建议:
rake db:migrate VERSION=20080906120000
版本号对应于迁移文件名的版本。
<强> [UPDATE] 强>
错误在于:
class AddLoginToUsers < ActiveRecord::Migration
def change
add_column :name, :password, :string
end
end
add_column具有以下要求:
add_column :table, :column_name, :column_type
确保表格是多元的。检查Schema.rb文件以查看表。由于这是users表,也许你的意思是:
add_column :users, :password, :string
如果您遇到架构问题,可以完全转储数据库并重新创建它。我将编辑适当的迁移(删除导致问题的迁移),然后通过运行转储并重新创建数据库:
rake db:drop db:create db:migrate
然后,您可以运行另一次迁移以添加新列。您可以在重新创建数据库之前将这些添加到现有迁移中。希望有所帮助。