Error to Migrate DB Rails - ruby-on-rails

Im trying to use this migration but i keep getting the error, like these
This is my Migration file
class CreateEmployees < ActiveRecord::Migration
def self.up
create_table :employees do |t|
t.string :name
t.date :hiredate
t.float :salary
t.boolean :fulltime , :default => true
t.integer :vacationdays
t.text :comments
end
end
def self.down
drop_table :employees
end
end
when i try to execute rake db:migrate get this error:
== CreateEmployees: migrating ================================================
-- create_table(:employees) rake aborted! An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "employees" already exists: CREATE TABLE
"employees" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name"
varchar (255), "hiredate" date, "salary" float, "fulltime" boolean
DEFAULT 't', "vacationdays" integer, "comments" text)
/Library/Ruby/Gems/2.0.0/gems/sql
ite3-1.3.8/lib/sqlite3/database.rb:91:in initialize'
/Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in
new'
/Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in
prepare'
/Library/Ruby/Gems/2.0.0/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in
execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in block in execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:425:inblock in log'
/Library/Ruby/Gems/2.0.0/gems/activesupport-4.0.0/lib/active_support/notifications/instrumenter.rb:20:in
instrument'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract_adapter.rb:420:inlog'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/sqlite3_adapter.rb:328:in execute'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/schema_statements.rb:190:in
create_table'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:625:in
block in method_missing'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in
block in say_with_time'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:597:in
say_with_time'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:617:in
method_missing'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:383:in
method_missing' /Users/YuriAguirre/Documents/Aptana Studio 3
Workspace/company/db/migrate/20130923140626_create_employees.rb:3:in
up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:534:in
up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:574:in
exec_migration'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:555:in
block (2 levels) in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:554:in
block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in
with_connection'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:553:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:709:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:959:in
block in execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in
block in ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
block in transactio n'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in
within_new_transact ion'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in
transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/transactions.rb:209:in transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:1005:in
ddl_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:958:in
execute_migration_in_transaction'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:920:in
block in migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
each'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:916:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:764:in
up'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/migration.rb:742:in
migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.0/lib/active_record/railties/databases.rake:42:in
`block (2 levels) in ' Tasks: TOP => db:migrate (See
full trace by running task with --trace)

Your employees tables is already created in your database. You need to fix it then you can do it in two ways 1. drop the database and create one more or 2. run the down migration then up migration
rake db:drop
rake db:create
rake db:migrate

As I've written in comment, you are receiving that error because of:
SQLite3::SQLException: table "employees" already exists
Which simply means you already have that column in your database. You can resolve that in few ways:
changing migration to modify instead of create:
class CreateEmployees < ActiveRecord::Migration
def self.up
add_column :employees, :name, :string
(...)
end
end
def self.down
remove_column :employees, :name
end
end
add another migration which will drop whole table (obviously, that one should be with earlier timestamp than modifying one so table will be dropped first and then created again):
class RemoveEmployees < ActiveRecord::Migration
def change
drop_table :employees
end
end
drop table by hand from console, but that's not a very good option as you should learn to use migrations for altering your database, of course that w'd be the fastest one (if you know how to use console) and will make no harm if that's just some learning project, but still, you should learn good habits when you can.
You might also want to use newer syntax (AFAIK) def change instead of self.up and self.down, less letters to type and less duplication ;)

Your migration is not completely run.You should rollback migration and migrate again.
rails db:rollback
rails db:migrate

You ran that migration already with an error in it. So the migration ran halfway, creating the table incompletely. Rollback that migration, and then run the migration again.

Related

How do I create this specific PostGres index in a Rails migration?

I’m using Rails 4.2.7 and PostGres 9.5. I’m trying to create a trqm index on my table (to facilitate LIKE searches) so I created this migration
class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
def change
CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);
end
end
This results in the below error when I run “rake db:migrate” …
StandardError: An error has occurred, this and all later migrations canceled:
undefined local variable or method `gin_trgm_ops' for #<AddTrqmIndexOnRaceTimes:0x007fdc34b40600>
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:664:in `block in method_missing'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:634:in `block in say_with_time'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:634:in `say_with_time'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:654:in `method_missing'
/Users/mikeb/Documents/workspace/runtrax/db/migrate/20161012185951_add_trqm_index_on_my_object_times.rb:3:in `change'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:608:in `exec_migration'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:592:in `block (2 levels) in migrate'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:591:in `block in migrate'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/connection_adapters/abstract/connection_pool.rb:292:in `with_connection'
/Users/mikeb/.rvm/gems/ruby-2.3.0/gems/activerecord-4.2.7.1/lib/active_record/migration.rb:590:in `migrate'
What’s the right way to create this index in a Rails migration?
You cannot add SQL directly in the ruby method change. Either use the add_index method, or if you need to go beyond the capabilities of the helper, use ActiveRecord to execute your query like this:
class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
def change
MyObjectTime.connection.execute("CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);")
end
end
Rails 5 adds support for specifying operator classes on expression indexes
for example:
def change
add_index :patients, 'lower(last_name) varchar_pattern_ops', name: "index_patients_on_name_unique", unique: true
end
source
ou need to make slight changes to your code. Wrap the sql query in execute.
class AddTrqmIndexOnRaceTimes < ActiveRecord::Migration
def change
execute "CREATE INDEX my_object_times_name_gin_trgm_idx ON my_object_times USING gin (name gin_trgm_ops);"
end
end

Added Column Not accessible in next migration file

I have added a column to my table via a migration, but I am not able to access the added column in the subsequent migration file.
When I execute rake db:migrate the migration aborts, but when I execute it again the migration succeeds, not sure what I am doing wrong. Any help would be appreciated. Thanks.
below is the code where i am adding column
**
class AddIsDispatchToUsers < ActiveRecord::Migration
def change
add_column :users, :is_dispatch, :boolean, :default=>false
end
end
**
Now, when i try to access the column in next migration file it fails.
The subsequent migration file has the code below
service_member = Member.create(:is_dispatch=>true)
And Here is the error that it produces
unknown attribute: is_dispatch/Users//.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/base.rb:1764:in `block in assign_attributes'
/Users//.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/base.rb:1758:in `each'
/Users//.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/base.rb:1758:in `assign_attributes'
/Users//.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/base.rb:1578:in `initialize'
/Users//.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/base.rb:508:in `new'
/Users//.rvm/gems/ruby-1.9.3-p484/gems/activerecord-3.1.12/lib/active_record/base.rb:508:in `create'
/Users//Desktop/RailsDevelopement//db/migrate/20161003121452_add_dispatch_services.rb:11:in `up'
Have you tried User.reset_column_information after add_column in same migration.

no implicit conversion of Symbol into Integer when migrating in production

So I'm working on an existing app and for some reason I was able to get it working and mirrored on my local environment however when I try migrating on heroku I get the below error, any ideas?
Error:
== 20141119113015 CreateReleasedInventoryStatus: migrating ====================
-- create_enum("eh_released_inventory_status", ["rejected", "pending", "allocated", "released", "transferred"])
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
no implicit conversion of Symbol into Integer/app/vendor/bundle/ruby/2.0.0/gems/power_enum-2.8.0/lib/power_enum/schema/schema_statements.rb:84:in `[]'
/app/vendor/bundle/ruby/2.0.0/gems/power_enum-2.8.0/lib/power_enum/schema/schema_statements.rb:84:in `create_enum'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:662:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:632:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:652:in `method_missing'
/app/db/migrate/20141119113015_create_released_inventory_status.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:606:in `exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:590:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/migration.rb:589:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.2.0/lib/active_record/connection_adapters/abstract/connection
Migration file:
class CreateReleasedInventoryStatus < ActiveRecord::Migration
def change
create_enum("eh_released_inventory_status", ["rejected", "pending", "allocated", "released", "transferred"])
end
end
I'm also using the following gem:
gem 'power_enum'
Your migration syntax seems to be incorrect for the power_enum gem that you're using.
The gem's README says the syntax uses a name, then an options hash, such as:
create_enum("status", {…})
Whereas you're using a name, then an array:
create_enum("eh_released_inventory_status", […])
As far as I can tell, the solution is to change your migration line from what you posted to the power_enum syntax. The README has a bunch of examples, and also looks like it's thorough and very well documented.
I think you migration should look like this
class CreateReleasedInventoryStatus < ActiveRecord::Migration
def change
create_enum :released_inventory_status, name_column: :status, description: true, name_limit: 5
["rejected", "pending", "allocated", "released", "transferred"].each do |status|
ReleasedInventoryStatus.update_enumerations_model do |klass|
#0.8.1 < version < 0.9.3 does not yield the klass
#in this case use:
#ReleasedInventoryStatus.create
#in place of klass.create
klass.create :name => status,
:description => status.capitalize
end
end
end
end
This will create the table released_inventory_statuses with status and description columns with a limit of 5 unique statuses. Then it will populate status and description for each item in you list through update_enumeration_model. This should work for your use case but I have not tested this theory. My answer is purely based on how create_enum works and then this section of the README
update_enumerations_model (since version 0.8.1)
The preferred mechanism to update an enumerations model in migrations and similar. Pass in a block to this method to to perform any updates.

Error trying to deploy to heroku (following ror tutorial) PG::Error: ERROR: column "password_digest" of relation "users" already exists

So I'm following the http://www.railstutorial.org/book, and evrything works fine locally (running sqlight3).
I get the following error when I try
heroku run rake db:migrate
This is what the error message looks like
Running rake db:migrate attached to terminal... up, run.4049
Migrating to AddPasswordDigestToUsers (20140817014655)
== 20140817014655 AddPasswordDigestToUsers: migrating =========================
-- add_column(:users, :password_digest, :string) PG::Error: ERROR: column "password_digest" of relation "users" already exists : ALTER
TABLE "users" ADD COLUMN "password_digest" character varying(255) rake
aborted! StandardError: An error has occurred, this and all later
migrations canceled:
PG::Error: ERROR: column "password_digest" of relation "users"
already exists : ALTER TABLE "users" ADD COLUMN "password_digest"
character
varying(255)/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in
block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:442:in block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.0.8/lib/active_support/notifications/instrumenter.rb:20:in
instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract_adapter.rb:437:in log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in
execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/schema_statements.rb:360:in
add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/postgresql/schema_statements.rb:395:in
add_column'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:629:in
block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in
block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:601:in
say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:621:in
method_missing'
/app/db/migrate/20140817014655_add_password_digest_to_users.rb:3:in
change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:575:in
exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:559:in
block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:558:in
block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in
with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:557:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:713:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:963:in
block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in
block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in
block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:211:in
within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/connection_adapters/abstract/database_statements.rb:203:in
transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/transactions.rb:209:in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:1009:in
ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:962:in
execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:924:in
block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in
each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:920:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:768:in
up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/migration.rb:746:in
migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.0.8/lib/active_record/railties/databases.rake:42:in
`block (2 levels) in ' Tasks: TOP => db:migrate (See
full trace by running task with --trace)
I already tried heroku pg:reset DATABASE_URL and then retrying.
Also tried adding /spec, /lib, /script, /features, /cucumber.yml to .slugignore
This is what my schema looks like:
ActiveRecord::Schema.define(version: 20140818041701) do
create_table "users", force: true do |t|
t.string "name"
t.string "email"
t.datetime "created_at"
t.datetime "updated_at"
t.string "password_digest"
t.string "remember_token"
t.boolean "admin", default: false
end
add_index "users", ["email"], name: "index_users_on_email", unique: true
add_index "users", ["remember_token"], name: "index_users_on_remember_token"
end
And this is what my password digest migration looks like
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
add_column :users, :password_digest, :string
end
end
You already have password_digest column in your table and with migration AddPasswordDigestToUsers you are trying to create another one that's why it creating issue. Remove migration AddPasswordDigestToUsers and try run rake db:migrate again. Sqlite3 not producing any error in such cases. if you write intege instead of integer in migration it will not produce any error if you use sqlite.
If you simply delete the password digest migration file, it will solve the short term problem of deploying to heroku but then it won't be available for collaborators to pull down and set up their database locally. They will run into issues and may create another migration file which creates the same problem.
I'm not sure the best solution, but this seems to be working out for me so far:
class AddPasswordDigestToUsers < ActiveRecord::Migration
def change
if Rails.env == "production"
else
add_column :users, :password_digest, :string
end
end
end

Rails migration error - Name can't be blank

I am using rails 2.x version. Am using mysql 5.0 database for back end process. I faced a migration error when i did the following steps.
Get the application code from github.
Run schema:create, load and seed process
After that add some data in the application.
Again Pull the code from the same branch.
After that i run db:migrate it throws the following error
-> rake db:migrate --trace
DEPRECATION WARNING: Rake tasks in vendor/plugins/acts_as_audited/tasks,
vendor/plugins/annotate_models/tasks,
vendor/plugins/app_version/tasks, vendor/plugins/bullet/tasks,
vendor/plugins/importer/tasks, vendor/plugins/mimetype-fu/tasks,
vendor/plugins/railsdav/tasks, vendor/plugins/rav/tasks,
vendor/plugins/simple_captcha/tasks, vendor/plugins/smart_table/tasks,
vendor/plugins/test_data_generator/tasks,
vendor/plugins/visualize_models/tasks, and
vendor/plugins/xss_terminate/tasks are deprecated. Use lib/tasks
instead. (called from
/usr/lib/ruby/gems/1.8/gems/rails-2.3.12/lib/tasks/rails.rb:10)
** Invoke db:migrate (first_time)
** Invoke environment (first_time)
** Execute environmentThu 11 Jul 2013 09:24:36 AM IST
** Execute db:migrate
== ConvertOrganizationToAwesomeNestedSet: migrating ==========================
Running sample patched rebuild process.
rake aborted!
An error has occurred, all later migrations canceled:
Validation failed: Name can't be blank
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/validations.rb:1102:in
save_without_dirty!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/dirty.rb:87:in
save_without_transactions!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in
save!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/connection_adapters/abstract/database_statements.rb:136:in
transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:182:in
transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in
save!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:208:in
rollback_active_record_state!'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/transactions.rb:200:in
save!'
/opt/sample/lib/awesome_nested_set_overrides.rb:25:in rebuild!'
/opt/sample/lib/awesome_nested_set_overrides.rb:22:incall'
/opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!'
/opt/ssample/lib/awesome_nested_set_overrides.rb:22:ineach'
/opt/sample/lib/awesome_nested_set_overrides.rb:22:in rebuild!'
/opt/sample/lib/awesome_nested_set_overrides.rb:32:incall'
/opt/sample/lib/awesome_nested_set_overrides.rb:32:in rebuild!'
/opt/sample/lib/awesome_nested_set_overrides.rb:29:ineach'
/opt/sample/lib/awesome_nested_set_overrides.rb:29:in rebuild!'
./db/migrate//20130102220216_convert_organization_to_awesome_nested_set.rb:6:in
up_without_benchmarks'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in
send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in
migrate'
/usr/lib/ruby/1.8/benchmark.rb:293:in measure'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:282:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:365:in
__send__'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:365:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:491:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:567:in
call'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:567:in
ddl_transaction'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:490:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:477:in
each'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:477:in
migrate'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:401:in
up'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.12/lib/active_record/migration.rb:383:in
migrate'
/usr/lib/ruby/gems/1.8/gems/rails-2.3.12/lib/tasks/databases.rake:112
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:228:in call'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:228:inexecute'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:223:in each'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:223:inexecute'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:166:in invoke_with_call_chain'
/usr/lib/ruby/1.8/monitor.rb:242:insynchronize'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:159:in invoke_with_call_chain'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/task.rb:152:ininvoke'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:143:in
invoke_task'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in
top_level'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in
each'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:101:in
top_level'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:110:in
run_with_threads'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:95:in
top_level'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:73:in
run'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:160:in
standard_exception_handling'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/lib/rake/application.rb:70:in
run'
/usr/lib/ruby/gems/1.8/gems/rake-10.0.3/bin/rake:33
/usr/bin/rake:19:inload'
/usr/bin/rake:19
Tasks: TOP => db:migrate
Organization.rebuild! - This is the code that calls the plugin.
It seems like there is some problem with awesome_nested_set plugin. I am struggle with this for past one week. Any comments would be appreciated.
EDIT - 1
class ConvertOrganizationToAwesomeNestedSet < ActiveRecord::Migration
def self.up
# add_column :party, :lft, :integer
# add_column :party, :rgt, :integer
Organization.reset_column_information
Organization.rebuild!
end
def self.down
remove_column :party, :lft
remove_column :party, :rgt
end
end
Model - Organization
class Organization < Party
attr_accessible :name,
:parent_id
default_scope :order => 'name'
acts_as_nested_set has_many :children, :class_name => 'Organization',
:foreign_key => "parent_id"
belongs_to :parent, :class_name =>'Organization', :foreign_key => "parent_id"
def display_name
name
end
You are using ActiveRecord model class in your migration. In short: don't.
This is likely to cause problems in the long run. If you have a migration that uses a model class, and than run it again later on, you'll be in trouble if anyone changes that model class.
Migrations are strictly for setting up the database, and should be constructed so that they will always behave in the same way - regardless of what the current state of your app's code is.
If you really need a migration to an ActiveRecord model: Define that class inside the migration file. This way it will be immutable to changes to the rest of the app.
Since you're getting
Validation failed: Name can't be blank
you must have a
validates_presence_of :name
somewhere, either on your class or in the plugin.
Maybe you have some invalid records in your database, and when you rebuild!, they are saved (and validated) and you get this error.

Resources