rename_column :table, :old_column, :new_column
You'll probably want to create a separate migration to do this. (Rename FixColumnName
as you will.):
script/generate migration FixColumnName
# creates db/migrate/xxxxxxxxxx_fix_column_name.rb
Then edit the migration to do your will:
# db/migrate/xxxxxxxxxx_fix_column_name.rb
class FixColumnName < ActiveRecord::Migration
def self.up
rename_column :table_name, :old_column, :new_column
end
def self.down
# rename back if you need or do something else or do nothing
end
end
For Rails 3.1 use:
While, the up
and down
methods still apply, Rails 3.1 receives a change
method that "knows how to migrate your database and reverse it when the migration is rolled back without the need to write a separate down method".
See "Active Record Migrations" for more information.
rails g migration FixColumnName
class FixColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
If you happen to have a whole bunch of columns to rename, or something that would have required repeating the table name over and over again:
rename_column :table_name, :old_column1, :new_column1
rename_column :table_name, :old_column2, :new_column2
...
You could use change_table
to keep things a little neater:
class FixColumnNames < ActiveRecord::Migration
def change
change_table :table_name do |t|
t.rename :old_column1, :new_column1
t.rename :old_column2, :new_column2
...
end
end
end
Then just db:migrate
as usual or however you go about your business.
For Rails 4:
While creating a Migration
for renaming a column, Rails 4 generates a change
method instead of up
and down
as mentioned in the above section. The generated change
method is:
$ > rails g migration ChangeColumnName
which will create a migration file similar to:
class ChangeColumnName < ActiveRecord::Migration
def change
rename_column :table_name, :old_column, :new_column
end
end
What I think is happening here is authenticate
is returning None
, therefore login
is using request.user
(an AnonymousUser
).
It looks like the information used to create the user is different to what you are passing to authenticate
. I would double check that the user is being saved correctly (especially the password).
Edit: From other answers I see you are using a custom user form. You should use django.contrib.auth.forms.UserCreationForm
, it's save()
method will set the password correctly.
The below doesn't work without manually setting the backend attribute, more trouble than it's worth, left for posterity
The easy out is to skip the authenticate step here (it doesn't add anything important)
user = UserForm.save()
login(request, user)
Best Answer
If this is your first migration or you just want to start over:
rm -Rf your_app/migrations/
python manage.py syncdb --migrate
Next migrations would need:
python manage.py schemamigration your_app --auto
python manage.py migrate your_app
That works for me :)