Ruby on rails migration error - ruby-on-rails

I installed a gem acts-as-taggable-on whenever i tried to run rails db:migrate it would output this:
C:\Sites\novosti>rails db:migrate
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:2:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
StandardError: Directly inheriting from ActiveRecord::Migration is not supported. Please specify the Rails release the migration was written for:
class ActsAsTaggableOnMigration < ActiveRecord::Migration[4.2]
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:2:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
So i just added [5.1] and it fixed the problem but another one appeared
C:\Sites\novosti>rails db:migrate
== 20170708123900 ActsAsTaggableOnMigration: migrating ========================
-- create_table(:tags)
-> 0.0028s
-- create_table(:taggings)
-> 0.0033s
-- add_index(:taggings, :tag_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
Index name 'index_taggings_on_tag_id' on table 'taggings' already exists
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:23:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
ArgumentError: Index name 'index_taggings_on_tag_id' on table 'taggings' already exists
C:/Sites/novosti/db/migrate/20170708123900_acts_as_taggable_on_migration.acts_as_taggable_on_engine.rb:23:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Index name on a table should be unique. So this is an error from db as the the index already exists. Please drop the index on the table of the db before migration or you can remove the add_index in migration if the index is already ok in the table

Related

rails: migration issue with paperclip gem

I'm trying to migrate the rails app but it fail. the issue in the paperclip gem migration.
I tried to use both way for migration but both of them not work properly:
class AddAttachmentImageToItems < ActiveRecord::Migration[6.1]
def self.up
add_attachment :items, :image
end
def self.down
remove_attachment :items, :image
end
end
also tried:
class AddAttachmentImageToItems < ActiveRecord::Migration[6.1]
def change
add_attachment :items, :image
end
end
after run rails db:migrate showing error:
/Users/zi/.rbenv/versions/3.0.1/lib/ruby/gems/3.0.0/gems/pry-byebug-3.8.0/lib/pry-byebug/control_d_handler.rb:5: warning: control_d_handler's arity of 2 parameters was deprecated (eval_string, pry_instance). Now it gets passed just 1 parameter (pry_instance)
== 20210713231704 AddAttachmentImageToItems: migrating ========================
-- add_attachment(:items, :image)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (given 4, expected 3)
/Users/zi/rails/stage_1/db/migrate/20210713231704_add_attachment_image_to_items.rb:3:in `up'
/Users/zi/rails/stage_1/bin/rails:5:in `<top (required)>'
/Users/zi/rails/stage_1/bin/spring:10:in `block in <top (required)>'
/Users/zi/rails/stage_1/bin/spring:7:in `<top (required)>'
Caused by:
ArgumentError: wrong number of arguments (given 4, expected 3)
/Users/zi/rails/stage_1/db/migrate/20210713231704_add_attachment_image_to_items.rb:3:in `up'
/Users/zi/rails/stage_1/bin/rails:5:in `<top (required)>'
/Users/zi/rails/stage_1/bin/spring:10:in `block in <top (required)>'
/Users/zi/rails/stage_1/bin/spring:7:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Found the issue in the ruby version, downgrade to 2.6.8 and migration work properly.

Ruby on Rails Adding comments to post

i created a comment controller in rails but if i run the server it gives an error in db migrate.
Caused by:
SQLite3::SQLException: table "comments" already exists
C:/Users/SIBI/Desktop/rails1/hello_world/db/migrate/20190516103017_create_comments.rb:3:in `change'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

my rails db:migrate isnt working

i tried running rake bd:migrate but it gave me an error and told me to run rails db:migrate RAILS_ENV=development instead but it gave me bunch of errors
C:\Sites\seekhostel.com>rails db:migrate RAILS_ENV=development
== 20180311182801 AddDeviseToUsers: migrating =================================
-- change_table(:users)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar DEFAULT '' NOT NULL
C:/Sites/seekhostel.com/db/migrate/20180311182801_add_devise_to_users.rb:7:in `block in up'
C:/Sites/seekhostel.com/db/migrate/20180311182801_add_devise_to_users.rb:5:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: email: ALTER TABLE "users" ADD "email" varchar DEFAULT '' NOT NULL
C:/Sites/seekhostel.com/db/migrate/20180311182801_add_devise_to_users.rb:7:in `block in up'
C:/Sites/seekhostel.com/db/migrate/20180311182801_add_devise_to_users.rb:5:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Caused by:
SQLite3::SQLException: duplicate column name: email
C:/Sites/seekhostel.com/db/migrate/20180311182801_add_devise_to_users.rb:7:in `block in up'
C:/Sites/seekhostel.com/db/migrate/20180311182801_add_devise_to_users.rb:5:in `up'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
i would love to know why it did so and how to fix it
By looking at error message I suspect two things happened here:
You already had users table (because new migration has change_table(:users), not create_table(:users) - devise generator recognize that)
You ran $ rails generate devise command and you didn't inspect autogenerated migration file.
You were supposed to remove line
t.string :email, null: false, default: ""
from 20180311182801_add_devise_to_users.rb file

Cannot use db Migrate, rake aborted error

I am working a new project in Codeanywhere (I am new on Ruby), but when I try to use "rake db : migrate" I have this error:
rake aborted!
Don't know how to build task ':' (see --tasks)
/home/cabox/.rvm/gems/ruby-2.1.2/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
(See full trace by running task with --trace)
Before this I use bundle update and after and nothing, and when I put "rake-T", db Migartion is on the tasks, when I try with --trace, pass the same.
I try before with rake db:migrate and the same happened
`rake aborted!
SyntaxError:
/home/cabox/workspace/blog/db/migrate/20170103233409_create_posts.rb:5: syntax error, unexpected ':'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `block in require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:232:in `load_dependency'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activesupport-4.1.6/lib/active_support/dependencies.rb:247:in `require'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:761:in `load_migration'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord- 4.1.6/lib/active_record/migration.rb:757:in `migration'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:752:in `disable_ddl_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:1044:in `use_transaction?'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:1036:in `ddl_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:990:in `execute_migration_in_transaction'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:952:in `block in migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:948:in `each'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:948:in `migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:807:in `up'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/migration.rb:785:in `migrate'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/activerecord-4.1.6/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
/home/cabox/.rvm/gems/ruby-2.1.2/gems/rake-12.0.0/exe/rake:27:in `<top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)` >
Does anyone know how to solve this problem?
In the migration file I have:
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.strind :
t.text :body
t.timestamps
end
end
end
I am pretty sure what is happening is you are inserting a space in there when you type it out. It needs to be
rake db:migrate
not
rake db : migrate
That error message is pretty common when there is a spelling error or some other kind of typing error when running the command.
Your migration has a typo as well, should be this
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :body
t.timestamps
end
end
end
Rake is a software task management tool, similar to Make, etc. in other systems
The generic syntax is
rake namesapce:task
In your case it is
rake db:migrate
Try doing rake db:reset if you want to rebuild your db again. It does 4 things for you
rake db:drop
rake db:create
rake db:migrate
rake db:seed

ActiveRecord::PendingMigrationError Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development

I have tried rake db:migrate and I can't seem to fix this problem. Anyone know where to begin? I have looked at most of the other pending migration error on StackOverflow and can't seem to find a solution.
Note: Running rails on Windows
After I run rake db:migrate:
DL is deprecated, please use Fiddle
-- add_index(:conversations, :sender_id)
-- add_index(:conversations, :sender_id)
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.conversations: CREATE INDEX "index_conversations_on_sender_id" ON "conversations" ("sender_id")
C:/Users/Jason/Documents/GitHub/TruTechEventPlanner/db/migrate/20151118013432_create_chats.rb:13:in `<class:CreateChats>'
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:1:in `<top (required)>'
C:in `disable_ddl_transaction'
SQLite3::SQLException: no such table: main.conversations
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:13:in `<class:CreateChats>'
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:1:in `<top (required)>'
C:in `disable_ddl_transaction'
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.conversations: CREATE INDEX "index_conversations_on_sender_id" ON "conversations" ("sender_id")
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:13:in `<class:CreateChats>'
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:1:in `<top (required)>'
C:in `disable_ddl_transaction'
SQLite3::SQLException: no such table: main.conversations
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:13:in `<class:CreateChats>'
C:/Users/Jason/Documents/GitHub/Planner/db/migrate/20151118013432_create_chats.rb:1:in `<top (required)>'
C:in `disable_ddl_transaction'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Resources