I used following code to add a field director to an existing movies table:
class CreateMovies < ActiveRecord::Migration
def up
create_table :movies do |t|
t.string :title
t.string :rating
t.text :description
t.datetime :release_date
# Add fields that let Rails automatically keep track
# of when movies are added or modified:
t.timestamps
end
add_column :movies, :director, :string
end
def down
drop_table :movies
end
end
I have already seen this, but the difrence is I am insisting to use
rake db:test:prepare command after i add my new field.
when i run rake db:test:prepare and then i run my cucumber, it gives me the erorr:
unknown attribute 'director' for Movie. (ActiveRecord::UnknownAttributeError)
this means that i failed to add the field director to the table movies,
So what is wrong here?
Try the following code:
rails g migration AddDirector
then, in the corresponding migration
def change
add_column :movies, :director, :string
end
Execute , rake db:migrate
In the Movies controller, add "director" in the movie_params
The db:test:prepare recreates the db using the db/schema.rb.
This will fix your issue:
bin/rake db:rollback
bin/rake db:migrate
When you do any migration, it gets saved in schema
Rollback that migration, make changes and then migrate it again
If for example 20150923000732_create_movies.rb migration is your last migration you can rollback with:
rake db:rollback
Otherwise you can simply down your specific migration with VERSION:
rake db:migrate:down VERSION=20150923000732
After rollback your migration, change your migration file and migrate again.
Related
My question is very much related to this one Should I delete migration after rollback.
I had my original migrate file 20140731141350_create_users.rb
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :email
t.string :password
t.timestamps
end
end
end
To which I needed to add a salt column, so I created the migration
20140804125449_add_salt_colum_to_users.rb
class AddSaltColumToUsers < ActiveRecord::Migration
def change
add_column :users, :salt, :string
end
end
But during development I realised the salt column wasn't necessary
and performed
rake db:migrate:down VERSION=20140731141350
Now I am left with an unused
20140804125449_add_salt_colum_to_users.rb migrate file.
My question is if I don't delete this migration file, where is this "down" status of this migration saved to? The migration file says add_column so if I run a db:migrate again how will it know that this specific file was migrated down?
db:migrate:down and db:rollback are similar. down reverts a database to a specified version, rollback - to the previous. To check is a migration applied Rails has a specific table called "schema_migrations" which stores timestamps of all applied migrations, so basically when you run db:migrate:down rails reverts the migration and remove the row from schema_migrations. So if you don't delete the migration file - rails will apply it in the next db:migrate
My User_ID miration says interger not integer.
class AddUserIdToPins < ActiveRecord::Migration
def change
add_column :pins, :user_id, :interger
add_index :pins, :user_id
end
end
I'm assuming I just can't change it from "interger" to "integer" using my text editor because it should be in my tables too.
Here is the way :
(A) First grab the specific migration number:
[shreyas#Arup-iMac rails_app_test (master)]$ rake db:migrate:status
database: app_development
Status Migration ID Migration Name
--------------------------------------------------
up 20150219075735 Create people
up 20150219085131 Add likes to persons
up 20150219114058 Add email to people
[shreyas#Arup-iMac rails_app_test (master)]$
(B) Now suppose, you want to edit the migration , 20150219085131, then do :
bin/rake db:migrate:down VERSION=20150219085131
(C) Then edit your migration and fix what you want to fix :
class AddUserIdToPins < ActiveRecord::Migration
def change
add_column :pins, :user_id, :integer
add_index :pins, :user_id
end
end
(D) And finally, again :
rake db:migrate:up VERSION=20150219085131
And you are done!
And If you are not able to run the current migration, then no worry, just change the file content by hand, and run rake db:migrate.
Edit your migration like this:
class AddUserIdToPins < ActiveRecord::Migration
def up
add_reference :pins, :user, index: true
end
def down
remove_index :pins, :user_id
remove_column :pins, :user_id
end
end
You rollback that migration:
bin/rake db:migrate:down VERSION=version_number
And try again:
bin/rake db:migrate:up VERSION=version_number
You can change it to :integer and then you have to run the migration, which will put the column in your db-tables:
rake db:migrate
If you try to run the migration like this it should show an error like:
type "interger" does not exist
I tried to create a new Link table and specified the necessary columns under migration.
under db/migrate
class CreateLinks < ActiveRecord::Migration
def change
create_table :links do |t|
t.integer :user_id
t.string :url
t.timestamps
end
end
end
class AddTitleToLink < ActiveRecord::Migration
def change
# add_column :links, :user_id, :integer
add_column :links, :title, :string
end
end
When I ran rails console, Link returned
Link(id: integer, created_at: datetime, updated_at: datetime, title: string)
It seems like user_id (the foreign key) and url are missing. Title, which was added later, is in the table.
Did I do anything wrong?
Did you perhaps run the CreateLinks migration before editing it to add the two fields? If so, you can change that file all day long and rake db:migrate will never re-run it. That would explain there being an empty table links, and the field you then added to it in the next migration.
You can step the database back by running rake db:rollback. Try doing that twice, then migrate again.
Can't see any reason for this not to work. Is it possible that you first ran:
rails g model Link
(which generated a migration AND RAN it)
and then you manually added the :url and :user_id?
Try running twice:
rake db:rollback
Then run again
rake db:migrate
which will catch up your manual modifications
HI I created a Ruby on rails migration file as follows and in the first stage I created tables
then I want to add columns and remove some columns and I modified it as follows
class CreateMt940Batches < ActiveRecord::Migration
def change
create_table :mt940_batches do |t|
t.string :account_number
t.string :transaction_reference_number
t.string :information_to_account_owner
t.string :file_name
t.binary :raw_data_transaction
t.string :sha1_checksum
t.timestamps
end
def self.down
remove_column :account_number, :transaction_reference_number, :information_to_account_owner
end
def self.up
add_column :mt940_batches, :created_by, :updated_by, :integer
end
end
end
but when I ran rake db:migrate nothing has happens. How to accomplish this task . I want to change the model already created as well from this migration file. Um looking a way to do this. Thank you in advance
You should add your remove / add column in a separate migration file.
class FooMigration < ActiveRecord::Migration
def down
remove_column :account_number, :transaction_reference_number, :information_to_account_owner
end
def up
add_column :mt940_batches, :created_by, :updated_by, :integer
end
end
Please note that your up and down method should be idem potent. You should be able to go from one to the other when calling rake db:migrate:down and rake db:migrate:up. This is not the case here.
However here, it seems that you want to achieve 2 different things in a single migration. If you want to add AND remove columns, consider moving each one in a different migration file:
Please read here for more details
You would end up with 2 migrations file like this:
class RemoveFieldsFromMt940Batches < ActiveRecord::Migration
def change
remove_column :mt940_batches, :account_number, :transaction_reference_number, :information_to_account_owner
end
end
class AddFieldsToMt940Batches < ActiveRecord::Migration
def change
add_column :mt940_batches, :created_by, :updated_by, :integer
end
end
Do not edit it if this migration already executed in production env create new one instead
if not you can use rake db:rollback, rollback migrations
Because this migration is already executed. you have to generate a new migration for adding and removing column in your table, i.e. you want to remove file_name from your table :
run this:
rails g migration RemoveFileNameFromCreateMt940Batches file_name:string
re-generate that column:
rails g migration AddFileNameToCreateMt940Batches file_name:string
Than run rake db:migrate it will remove column and add column again to your table.
Hope it will help. Thanks.
Create another migration file with removed column list
def change
remove_column :account_number, :transaction_reference_number, :information_to_account_owner
end
Create one migration file with added column list
def change
add_column :mt940_batches, :created_by, :updated_by, :integer
end
Do not alter the create table migration file. Other wise data saved in the file will be lost.
If data lost is not important for you, then just remove the table using rake db:migrate:down version=<your migration file version>
And change the migration file
then run
db:migrate:up version=<your migration file version>
I am trying to install the Ancestry gem but I am having problems with rake db:migrate.
I am following the instructions on the Ancestry github page. After I have done rails g migration add_ancestry_to_message ancestry:string
I am editing the migration file (following railcast #262) to be:
class AddAncestryToMessage < ActiveRecord::Migration
def self.up
add_column :messages, :ancestry, :string
add_index :messages, :ancestry
end
def self.down
remove_index :messages, :ancestry
remove_column :messages, :ancestry
end
end
When I then run rake db:migrate I am getting the following error:
== AddAncestryToMessage: migrating ===========================================
-- add_column(:messages, :ancestry, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: Shipmgr: ALTER TABLE "Message" ADD "ancestry" varchar(255)
Tasks: TOP => db:migrate
I have tried this on a newly created rails app and on an existing rails app but I am still unable to get this to work. Does anyone have any advice on this issue?
You should try changing the migration class name to the pluralized (table) form 'Messages':
class AddAncestryToMessages < ActiveRecord::Migration
or, more accurately, change the migration generator command to:
rails g migration add_ancestry_to_messages ancestry:string