In a new Rails 6 project, I have a table named object_classes with a column named ClassList_id. From schema.rb:
create_table "object_classes", force: :cascade do |t|
t.string "name"
t.integer "ClassList_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.index ["ClassList_id"], name: "index_object_classes_on_ClassList_id"
end
I've realized that the column should be named class_list_id to conform with Rails expected naming convention. Therefore, I have generated a new migration:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
rename_column :object_classes, :ClassList_id, :class_list_id
end
end
However, when I run this migration, I get the following error:
/bin/bash -c "env RBENV_VERSION=2.6.1 /home/asfarley/.rbenv/libexec/rbenv exec bundle exec ruby /home/asfarley/imgseq/bin/spring rails 'db:migrate'"
== 20200716060501 FixColumnName: migrating ====================================
-- rename_column(:object_classes, :ClassList_id, :class_list_id)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Caused by:
SQLite3::SQLException: no such table: main.ClassLists
/home/asfarley/imgseq/db/migrate/20200716060501_fix_column_name.rb:3:in `change'
/home/asfarley/imgseq/bin/rails:9:in `<top (required)>'
/home/asfarley/imgseq/bin/spring:15:in `require'
/home/asfarley/imgseq/bin/spring:15:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Process finished with exit code 1
What am I doing wrong here? I'm looking for an explanation that addresses specifically what is wrong here, so that I can understand how to avoid this in the future.
Hard to know whether the fault is Rails or SQLite, but the issue seems to be that I had a foreign key defined for my object_classes table pointing to a table that didn't exist. With this migration, I was able to fix the foreign key constraint and rename the column:
class FixColumnName < ActiveRecord::Migration[6.0]
def change
remove_foreign_key :object_classes, :ClassLists
rename_column :object_classes, :ClassList_id, :class_list_id
add_foreign_key :object_classes, :class_lists
end
end
your syntax is ok so the error must be a reserved name error. may try and remove the column and creating the column again, watch out for capital letters on migrations for they can unnecessarily complicate your code. try this:
rails g migration RemoveClassListIdFromObjectClasses
that will created a migration kinda like this:
def drop
remove column :object_classes, :class_lis_id
end
and then run
rails g migration AddClassListIdToObjectClasses
the migration should look something like this:
def change
add_column :object_classes, :class_list_id
end
remember to add the data type and anything else you may need.
there you can check the migration it created and you can change it to maybe add the data type or maybe change the capitalization of the column and you will be set to go.
note: if it works i would appreciate if you accepted the answer, Thanks!
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)
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
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
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)