当我在rails中运行迁移时,我收到一条sql错误,说没有这样的列:admin_users.password存在。当我显示场景时,它显然在那里。我确定这是因为我错过了某个地方。在这种情况下,理解错误对我来说比解决问题要重要得多,因为我在数据库处于破坏状态时不断陷入困境。
First Migration:
class CreateUsers < ActiveRecord::Migration
def up
create_table :users do |t|
t.column "first_name", :string, :limit => 25
t.string "last_name", :limit => 50
t.string "email", :default => "", :null => false
t.string "password:", :limit => 40
# t.datetime "created_at"
# t.datetime "updated_at"
# The above two are created automatically by the below command
t.timestamps
end
end
def down
drop_table :users
end
end
Second Migration:
class AlterUsers < ActiveRecord::Migration
def up
rename_table("users", "admin_users")
add_column("admin_users", "username", :string, :limit => 25, :after => "email")
change_column("admin_users", "email", :string, :limit => 100)
rename_column("admin_users", "password", "hashed_password")
puts "*** Adding an index is next***"
add_index("admin_users", "username")
end
def down
remove_index("admin_users", "username")
rename_column("admin_users", "hashed_password", "password")
change_column("admin_users", "email", :default => "", :null => false)
remove_column("admin_users", "username")
rename_table("admin_users", "users")
end
end
=>== CreateUsers: migrated (0.0163s) ===========================================
== AlterUsers: migrating =====================================================
-- rename_table("users", "admin_users")
-> 0.0034s
-- add_column("admin_users", "username", :string, {:limit=>25, :after=>"email"})
-> 0.0158s
-- change_column("admin_users", "email", :string, {:limit=>100})
-> 0.2273s
-- rename_column("admin_users", "password", "hashed_password")
rake aborted!
An error has occurred, all later migrations canceled:
No such column: admin_users.password
答案 0 :(得分:3)
这是因为您在第一次迁移中使用冒号调用了列password:
。
我打赌那是因为rails generate model
命令行中的拼写错误。这是非常万无一失的。