Ancestry db:migrate - ruby-on-rails

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

Related

How can I fix a typo in data migration?

I'm doing the Michael Hartl Ruby on Rails Tutorial, and ran into this problem at the end of Chapter 11.
I did a migration to add activation_digest, activated, and activated_at columns to my data model.
$ rails generate migration add_activation_to_users \
> activation_digest:string activated:boolean activated_at:datatime
As you can see, I wrote datatime instead of datetime.
Now my migration file looks like this:
class AddActivationToUsers < ActiveRecord::Migration[5.1]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated, :boolean, default: false
add_column :users, :activated_at, :datatime
end
end
Can I simply fix this error by editing the migration file? Or should I re-run the migration at the command line? Is there a better way to do this?
rake db:rollback
will rollback the migration, then you can fix the typo and run
rake db:migrate
again to re run the migration.
You can edit the migration file before you run the migration.

Rails 5: How to remove a column from a database?

What is the command for removing an existing column from a table using migration?
The column I want to remove is: country:string
From the table: sample_apps
To remove a column with migration:
rails g migration Remove..From.. col1:type col2:type col3:type
In your case:
rails g migration RemoveCountryFromSampleApps country:string
This will generate the following migration in Rails 5.0:
class RemoveCountryFromSampleApps < ActiveRecord::Migration[5.0]
def change
remove_column :sample_apps, :country, :string
end
end
Create migration file:
$ rails generate migration RemoveCountryFromSampleApps country:string
In generated migration file:
class RemoveCountryFromSampleApps < ActiveRecord::Migration
def change
remove_column :sample_apps, :country, :string
end
end
Then run:
rake db:migrate
To remove a column(country here) from table(sample_case)
rails generate migration RemoveCountryfromSampleCase country:string
Above command should generate a file YYYYMMDDHHMMSS_remove_countryfrom_sample_case.rb. under db/migrate folder
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
remove_column :sample_case, :country, :string
end
end
In my case (I was doing it for more than two columns) only this appears
class RemoveCountryFromSampleCase < ActiveRecord::Migration[5.0]
def change
end
end
remove_column line was not there so I added it manually and then fired the command
rails db:migrate
and it worked for me.
References https://stackoverflow.com/a/2963582
&
Ruby guide on Active Record migration
https://edgeguides.rubyonrails.org/active_record_migrations.html
If want to remove the index too, you do with migration too:
rails g migration remove_post_id_from_comments post_id:integer:index
migration file:
class RemovePostIdFromComments < ActiveRecord::Migration
def change
remove_index :comments, :post_id
remove_column :comments, :post_id, :integer
end
end
then run: rake db:migrate
Do you need to specify the type?
Why not just remove_column :sample_apps, :country or remove_column :comments, :post_id for the samples here? Seems to work and removes a chance for error (text or string).

Adding a column to an existing table

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.

My User_ID miration says interger not integer. How do I change it?

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'm having trouble dropping a migration during a db:reset. Are my migrations incorrect?

I'm getting this error when I run a db:reset
rake aborted!
ActiveRecord::StatementInvalid: PG::DependentObjectsStillExist: ERROR: cannot drop table seasons because other objects depend on it
DETAIL: constraint weeks_season_id_fk on table weeks depends on table seasons
HINT: Use DROP ... CASCADE to drop the dependent objects too.
: DROP TABLE "seasons"
The most recent migrations I added were...
class AddSeasonIdToWeek < ActiveRecord::Migration
def change
add_column :weeks, :season_id, :integer, null: false, index: true
end
end
and
class AddForeignKeySeasonsToWeeks < ActiveRecord::Migration
def change
change_table :weeks do |a|
a.foreign_key :seasons
end
end
end
What do I need to do to get past this error?
Try using this in rails 4:
rake db:rollback
twice
In older versions of rails (probably 3 and before)
rake db:migrate:down
twice

Resources