add default value to column after migration not working - ruby-on-rails

I created a column in a table as normal and did rake db:migrate. I then realised that I needed to set a default value for the column. After looking on stackoverflow I found several people that said all I had to do was edit the migration file so that this
def change
add_column :accounts, :equipment_visible, :boolean
end
turns into this
def change
add_column :accounts, :equipment_visible, :boolean, default: true
end
I did this then ran rake db:migrate again but nothing happened.
I kept looking on for answers as I assume it didn't work because I had already run rake db:migrate.
I then ran another migration and put this into the file;
def change
def up
change_column :accounts, :equipment_visible, :boolean, default: true
end
def down
change_column :accounts, :equipment_visible, :boolean, default: nil
end
end
but still nothing has happened....does anyone know how I can go about this now?
Many thanks
*EDIT
The schema for the table is like this:
create_table "accounts", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
t.boolean "crafts_visible", default: true
t.boolean "equipment_visible"
end
I am trying to get the "equipment_visible" to have default: true after it like the "crafts_visible"

You have to rollback your migration and migrate db again:
bundle exec rake db:rollback
bundle exec rake db:migrate
or just run redo task:
bundle exec rake db:migrate:redo

I think you want:
change_column_default :accounts, :equipment_visible, true

Related

My database isn't running all of my migrations, but says the schema number has

I was trying to fix a bad migration. I reset my database a couple times and it's just causing further problems, namely that not all my migrations are even running now.
Below are all the migrations, the one that was broken is the one yet to be run. But when I try to do a rake db:migrate I get this error:
undefined method `to_sym' for
nil:NilClass/usr/local/lib/ruby/gems/2.2.0/gems/activerecord-4.2.1/lib/active_record/connection_adapters/abstract/schema_definitions.rb:258:in
`column'
This is an issue in itself but what is most confusing to me is that the migration to create the simulation table just isn't running. My scheme looks like this:
ActiveRecord::Schema.define(version: 20150806192507) do
create_table "users", force: :cascade do |t|
t.string "email", limit: 96, default: "", null: false
t.string "encrypted_password", limit: 60, default: "", null: false
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
end
Any suggestions as to:
The simulations table is not being created when I do a reset and re-migrate.
The final migration to remove the verdict column is failing.
These are the migrations I have:
1.
class DeviseCreateUsers < ActiveRecord::Migration
def change
create_table(:users) do |t|
## Database authenticatable
t.string :email, :null => false, :default => "", limit: 96
t.string :encrypted_password, :null => false, :default => "", limit: 60
t.timestamps
t.index :email, unique: true
end
end
end
2.
class CreateSimulations < ActiveRecord::Migration
def change
# Needs the hash column worked out before this is run
create_table :simulations do |t|
t.integer :x_size
t.integer :y_size
t.string :verdict
t.string :arrangement
end
add_reference :simulations, :user, index: true
end
end
3.
class AddOpinionToSimulation < ActiveRecord::Migration
def change
add_column :simulations, :opinion, :hash
end
end
Finally, this is the bad one I was trying to run that started these problems. I have deleted the file to stop it from attempting to be migrated:
class RemoveVerdictFromSimulations < ActiveRecord::Migration
def change
remove_column :simulations, :verdict
end
end
Any suggestions?
There is no table simulations in your schema. If if it's not there, then you'll never be able to run that third migration.
First, refresh your database schema to make sure it's accurate in relation to your database with rake db:schema:dump
If there is indeed no simulations table, then first make sure that migration 2 succeeds.
Check schema_migrations table. If there are two rows there, then rails will think that the simulations table got created already, just delete the last entry from this table and migrate again making sure that the simulations table gets created this time, then try again with migration 3
My solution to this particular error was to go into my SQL database, delete every single version in the schema table. Then I did a
rake db:drop
I then walked through each version to make sure they migrated in order using:
rake db:migrate VERSION=<VERSION_NUMBER_FROM_EARLIEST_TO_LATEST>
If I ran into an error about a table not existing using:
rake db:prepare
Seemed to fix it aside from the last problem migration.

Rails migration schema issue

In my rails App I have three models/tables in the database (users, registrants, events). I was setting up some Capybara/RSpec tests when I ran into an issue saying it could not find the events table.
Now the events model has been working fine, I've been able to create events, go into the rails console and query things such as Events.all or Events.find(1). What is confusing me is my schema. My schema has
create_table "registrations", force: true do |t|
t.string "first_name"
...
end
create_table "users", force: true do |t|
t.string "email", default: "", null: false
...
end
But there is no reference to the events table. It's been some time since I generated the model for Events but here is the migration:
class CreateEvents < ActiveRecord::Migration
def change
create_table(:events) do |t|
t.string :name
t.string :location
t.timestamps
end
end
end
I have ran commands such as rake db:reset and rake db:migrate to make sure all my migrations are current. Any ideas why the events table is not in the schema?
UPDATE:
So I've deleted the DB and Schema and began migrating each migration one at a time. The events table is there when I run the migration to create it, but after I run the following command the table disappears from the schema:
class AddingAssociationsToEventsUserModels < ActiveRecord::Migration
def change
add_column :events, :belongs_to, :user, index: true
end
end
Here is the migration being run:
rake db:migrate:redo VERSION=20150518132834
== 20150518132834 AddingAssociationsToEventsUserModels: migrating =============
-- add_column(:events, :belongs_to, :user, {:index=>true})
-> 0.0006s
== 20150518132834 AddingAssociationsToEventsUserModels: migrated (0.0006s) ====
That's because test database and development database are not matched.
Run:
RAILS_ENV=test rake db:drop && rake db:create && rake db:migrate
or
RAILS_ENV=test rake db:test:prepare
or
RAILS_ENV=test rake db:reset
Any of the above should work.
Ok so the issue was a mistake in my migration file. Here is what I changed my migration to:
class AddingAssociationsToEventsUserModels < ActiveRecord::Migration
def change
add_reference :events, :user, index: true
end
end
Now the events table is in my Schema and the test can find the table and is passing!

No output after running rake db:migrate

I'm struggling here with the db migration for the acts_as_commentable_with_threading.
After generating the migration rails generate acts_as_commentable_with_threading_migration I proceeded to add the comment table rake db:migrate. Nothing happened, no error message, just returned to the regular prompt.
Looking at other response to this problem I tried rake db:migrate VERSION= # version number.
Yet here I get an Error response ActiveRecord::UnknownMigrationVersionError:
I must be doing something extremely wrong here since the computer doesn't validate the existence of my comment migration...
UPDATE from #Tiago answer
Ran rails generate migration acts_as_commentable_with_threading_migration
I had to manually create the migration by adding this code to the migration file. Then, thedb:migration worked perfectly.
Why wasn't it working in the first place? The Documentation clearly indicate to run rails generate acts_as_commentable_with_threading_migration.
Just create a migration for yourself as indicated in the gem's page:
rails g migration acts_as_commentable_with_threading_migration
And paste that to the file:
class ActsAsCommentableWithThreadingMigration < ActiveRecord::Migration
def self.up
create_table :comments, :force => true do |t|
t.integer :commentable_id, :default => 0
t.string :commentable_type
t.string :title
t.text :body
t.string :subject
t.integer :user_id, :default => 0, :null => false
t.integer :parent_id, :lft, :rgt
t.timestamps
end
add_index :comments, :user_id
add_index :comments, [:commentable_id, :commentable_type]
end
def self.down
drop_table :comments
end
end

rails model default value

Here is my model migration
class CreateSwimmingClassschedules < ActiveRecord::Migration
def change
create_table :swimming_classschedules do |t|
t.integer :slot_id
t.integer :coach_id
t.integer :level_id , :default => 1
t.string :note
t.timestamps
end
end
end
I expect after I call
Swimming::Classchedule.create(:coach_id=>8)
It will generate a default level_id in table. But somehow it didn't work. I'm in the dev environment using SQLite.
I added
:default => 1
after I ran
rake db:migrate
Does it matter?
Something I am missing?
Adding that line after you have run your migrations will not make the change.
Your syntax is correct but you will need to run a migration with that addition. Consider making a separate migration file like this:
class ChangeLevelId < ActiveRecord::Migration
def change
change_column :swimming_classschedules, :level_id, :integer, :default => 1
end
end
If you added :default => 1 AFTER you did a rake db:migrate, you will need to do a rake db:rollback and do a remigration of your database. That should do the trick.
Is it Classchedule or Classschedule ?
When I have a problem with my database, I recreate it :
rake db:drop
rake db:create
rake db:migrate

remove_column not removing column or giving any errors in rails

I have a sqlite3 db in a rails app with the following schema
ActiveRecord::Schema.define(:version => 20100816231714) do
create_table "comments", :force => true do |t|
t.string "commenter"
t.text "body"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "posts", :force => true do |t|
t.string "name"
t.string "title"
t.text "content"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "tags", :force => true do |t|
t.string "name"
t.integer "post_id"
t.datetime "created_at"
t.datetime "updated_at"
end
end
I began with a Post :has_many relationship with tags so each tag has a post_id reference.
I now want to change this relationship to a 'has_and_belongs_to_many', I know i have to create the joins table etc.... this isn't a problem and is working
The problem comes in when i try remove the post_id form the tags table. My migration looks like this:
class RemoveFieldsToTags < ActiveRecord::Migration
def self.up
remove_column :tags, :post_id
end
def self.down
add_column :tags, :post_id, :references
end
end
When I run rake db:migrate and rake db:migrate:up VERSION= Nothing happens when I run rake db:migrate:down VERSION= I get column:
SQLite3::SQLException: duplicate column name: post_id: ALTER TABLE "tags" ADD "post_id" references
Any one know whats going on?
It sounds as if Rails thinks your DB is up to date (given that nothing happens when you run db:migrate). You can get into this state if you've modified your migration after applying it (common during development).
Have you tried running db:migrate on a fresh db (note this will wipe your database)?
rake db:drop db:create db:migrate
Like avaynshtok mentions above, it sounds like rails thinks your migrations are up to date (as in, they have all been applied), but to you they are not (the post_id column is still on the tags table).
A common 'workaround' to deal with this situation without having to wipe your database is commenting out the 'down' method of your migration and running
rake db:migrate:redo
Given the 'down' is commented out, it won't try to add the column again, so it will proceed to reapply the 'up' method, removing your 'post_id' column. You can then remove the comment on the 'down' method and it should all be good.
PS. You might want to look into using a 'has_many :through' type of relationship instead of 'has_and_belongs_to_many' as well.
I had a similar problem to the op, but had to manually delete the databases and then run
rake db:create db:migrate
rake db:migrate:redo
and
rake db:drop
did not work for me as it kept saying "db/test.sqlite3 already exists".

Resources