Hi I have a general migration problem:
When I create migrations like this:
class RenameColumn < ActiveRecord::Migration
def change
rename_column :users, :hotel_stars, :rating_stars
rename_column :users, :restaurant_stars, :price_stars
end
end
and change the code in the Model-,View- and Controller file accordingly(I dont create new Model etc.):
ie.
Model: attr_accessible :rating_stars, :price_stars
(instead of :hotel_stars, :restaurant_stars )
Controller: #rating = current_user.rating_stars
When I now run the migration (rake db:migrate) -> it works!
But after a rake db:drop, rake db:create, rake db:migrate it doesn't anymore!
What is wrong with this migration? How can you create migrations that are working WITH and WITHOUT resetting the database?
Thanks!!
I think your issue is that rake db:create does not rebuild your database from schema.rb. For that you need to do rake db:setup instead of rake db:create. In any case I would try rake db:reset instead of drop/create as I believe that will accomplish what you want to do in one step.
Type rake -T for a list of available tasks and what they do.
Also see here for more information on rails migrations:
http://guides.rubyonrails.org/migrations.html
Did you ever manually alter the state of your database and/or modify a migration after it was run? If so, you messed up the state of migrations.
You "could" alleviate this by adding a migration that gets your database where your new migration expects it to be, or you could fix the migration that was altered before.
Depending on how many other developers you have, I would go for the latter in this case.
Related
I want to remove column and index in activities table. To do so I've got below migration:
class RemoveUnnecessaryJourneyActivitiesRelations < ActiveRecord::Migration[6.0]
def change
remove_index :activities, :cms_journey_id
remove_column :activities, :cms_journey_id, :bigint
end
end
When I run rake db:migrate it removes this column and index as well but when I want to reset my DB by rake db:drop db:create db:migrate db:seed or bin/rails db:setup I'm getting an error:
rake aborted!
ActiveModel::UnknownAttributeError: unknown attribute 'cms_journey_id' for Activity.
How was the attribute added in the first place? If it was via a migration it looks like that file had been deleted.
You have two options - if you want to get the above migration working, you'll need to insert one before it (with an earlier timestamp) that creates the attribute, and then run this migration to remove it.
Of course, the easier option is to just delete the above migration. Because there is no migration that creates the attribute, you can delete the above migration, run rake db:migrate, have a look at your db.schema.rb file, and you'll see no column called cms_journey_id on the activities table.
I have entrees and snapshots. An entree has_many snapshots. I forgot to add the foreign key to snapshots. So I later added migration:
$ rails generate migration add_snapshot_ref_to_entrees snapshot:references
Migration file looked like this:
class AddSnapshotRefToEntrees < ActiveRecord::Migration[5.0]
def change
add_reference :entrees, :snapshot, foreign_key: true
end
end
This did not do what I want. It added an snapshot_id to entrees, rather than adding an entree_id to snapshots.
So I run the rollback:
bundle exec rake db:rollback
It correctly moves the following from schema.rb:
add_foreign_key "entrees", "snapshots"
And also correctly rolls back the database itself. But the migration file remains in db/migrate. Am I suppose to remove this myself or did not complete its job?
Yes, you need to remove this file manually. Regarding your association, it should be as below:
class AddSnapshotRefToEntrees < ActiveRecord::Migration[5.0]
def change
add_reference :snapshots, :entree, foreign_key: true
end
end
It's not supposed to.
rails destroy migration add_snapshot_ref_to_entrees
In order to rollback a specific migration use:
rake db:migrate:down VERSION=YOUR_MIGRATION_VERSION
For Example:
rake db:migrate:down VERSION=20100905201547
To find the version of all migrations, you can use this command:
rake db:migrate:status
rake db:migrate:down and rollback worked on database label it's not removing file from db/migrate.
You need to remove direct from db/migrate folder.
I tried to create a migration to add roles to my user tables but i accidentally typed AddRolesToUsers instead of AddRoleToUser. So i tried creating a new migration with the correct AddRoleToUsers but when i tried to run rake db:migrate i got an error :
SQLite3::SQLException: duplicate column name: role: ALTER TABLE "users" ADD "role" integer/Users/miguel/.rvm/gems/ruby-2.2.1/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize'
I tried rake db:migrate:down VERSION= do delete the one I had to type on but i keep getting the same error . PS: i deleted the migration file manually after running rake db:migrate:down VERSION=
rails g migration AddRoleToUsers role:integer
migration file :
class AddRoleToUsers < ActiveRecord::Migration
def change
add_column :users, :role, :integer
end
end
When you ran the first migration, the role column was added to the Users table. The error on the second migration tells you that much.
To clear the migration pending error, you need to comment out the add_column statement in the new migration.
i.e,
class AddRoleToUsers < ActiveRecord::Migration
def change
# add_column :users, :role, :integer
end
end
Then run the migration. This way, the new migration should run successfully.
You can now uncomment it and delete the previous migration, so that when you deploy, only the newer migration is run and the role column is added successfully.
In that case right click and delete both migration files and start again. The error exists because it thinks you want to add two columns both named role to your users table
UPDATE
In that case, if role already exists in your users table, a migration must've been successfully run and there is no need to run another. As long as the role column is there, trying to add another column called role will always give you an error. If you wanted to test that you are still able to add new columns you can always check by creating a test migration AddSomethingToUsers something:string and rake db:migrate to test, then rake db:rollback to undo..but it all seems like it's worked so I probably my wouldn't mess with it too much.
I have the following migrations
Problem is that rake db:migrate is not executing the first migration and no users table is created.
What could be the reason for this?
What could be the reason for this?
Main reason is probably that you've already ran the migration - or perhaps later migrations - and Rails therefore does not think it needs to run it.
A good way to see if this is the case is to open your db/schema.rb file:
You'll see the latest migration your schema is running. If this supersedes the one you're trying to invoke, it will not run.
--
Fixes
You could generate a new migration, and copy the code over:
$ rails g migration AddUsers2
You'd then add the following:
#db/migrate/_____.rb
class AddUsers2 < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
t.timestamps
end
end
end
Alternatively, you could wipe your DB and start again. This can be achieved using rake schema:load. THIS WILL WIPE ALL DATA AND START AGAIN
I use this command to create an avatar column in my postgresql database.
rails g migration add_avatar_to_users avatar:string
rake db:migrate
I have an image column i would like to get rid of in the same database. I don't know the command to do so. I dont need the data and that column anymore.
I tried
rails destroy migration add_image_to_users image:string
rake db:migrate
But it did not fix the problem =/
rails g migration remove_image_from_users
That will generate a new migration, and then inside the change method you can write:
remove_column :users, :image