Rails rake tasks aborting - ruby-on-rails

I am having trouble running any rake task for my Rails application, and no matter what task I run (rake db:migrate, rake db:reset, etc), I get the following error:
rake aborted!
ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: pages: SELECT "pages".* FROM "pages"
I continue getting this error - no matter what rake task I run, and also when I try to run the server:
rails s
gets the following error
Exiting
/Users/terencedevine/.rvm/gems/ruby-2.1.2/gems/sqlite3-1.3.11/lib/sqlite3/database.rb:91:in `initialize': SQLite3::SQLException: no such table: pages: SELECT "pages".* FROM "pages" (ActiveRecord::StatementInvalid)
Everything I find online suggests using rake db:reset but that returns the same error.
One of my more recent migrations I ran was a XXXX_create_pages.rb which has the following code:
class CreatePages < ActiveRecord::Migration
def change
create_table :pages do |t|
t.string :name, null: false, unique: true
t.string :title, null: false
t.text :body
t.timestamps null: false
end
end
end
Any help is greatly appreciated! Thanks!
UPDATE

You need to make sure you actually execute your migrations.
Try rake db:migrate then try to run your server or console again.

Make sure to run rake db:create and then rake db:migrate and that should work.

ActiveRecord::StatementInvalid: SQLite3::SQLException: no such table: pages: SELECT "pages".* FROM "pages"
From the error message, it's obvious that the pages table does not exist in your database right now. It got deleted somehow even if you did not delete it knowingly.
So, you should create the pages table again by running the corresponding migration:
rake db:migrate
In case your schema version exceeded to migration XXXX_create_pages.rb then rename you migration with greatest timestamp.
Eg.
Your page migration is
20151130203912_create_pages.rb
If your current schema version is
ActiveRecord::Schema.define(:version => 20151211175046)
Then pages migration must be 20151230203912_create_pages.rb
I hope it would be helpful.

Related

Deployment issues after changing schema tables in ROR Heroku

After changing my GuideIndustry model to ConnectIndustry through find and replace, I have been getting the error below on the release log preventing me from deploying my rails app onto heroku. Is there anyway to have rails override all the files heroku is using? My local copy of all the files and the schema are working just fine. I have tried using the heroku pg:reset and then heroku run rake db:migrate.
== 20190215035620 AddArticleIdToConnectIndustry: migrating ====================
-- add_column(:connect_industries, :article_id, :integer)
(2.4ms) ALTER TABLE "connect_industries" ADD "article_id" integer
(1.0ms) ROLLBACK
(1.2ms) SELECT pg_advisory_unlock(3329669382293610510)
rails aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::UndefinedTable: ERROR: relation "connect_industries" does not exist
Update:
Through find and replace and a manual file name change, i edited the create GuideIndustry table migration to be as follows:
class CreateConnectIndustries < ActiveRecord::Migration[5.1]
def change
create_table :connect_industries do |t|
t.integer :guide_id
t.integer :industry_id
t.timestamps
end
end
end

Error ActiveRecord::PendingMigrationError

I am following Michael Hartl's book and I am getting this error when I run the server:
I am getting ActiveRecord::PendingMigrationError when I run the server, this shows up:
Migrations are pending. To resolve this issue, run: bin/rails db:migrate RAILS_ENV=development
Please I've been stuck in this error for so long.
When I type $ RAILS_ENV=development rake db:migrate I get this error:
== 20161209073230 AddActivationToUsers: migrating =============================
-- add_column(:users, :activation_digest, :string) rake aborted! StandardError: An error has occurred, this and all later migrations
canceled:
SQLite3::SQLException: duplicate column name: activation_digest: ALTER
TABLE "users" ADD "activation_digest" varchar
(required)>' Tasks: TOP => db:migrate (See full trace by running task
with --trace)
test/mailers/previews/user_mailer_preview.rb
UserMailerPreview < ActionMailer::Preview
# Preview this email at
http://localhost:3000/rails/mailers/user_mailer/account_activation
def account_activation
user = User.first
user.activation_token = User.new_token
UserMailer.account_activation(user) end
# Preview this email at
http://localhost:3000/rails/mailers/user_mailer/password_reset def
password_reset
UserMailer.password_reset end
end
Schema.rb:
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20161123005710) do
create_table "users", force: :cascade do |t|
t.string "name"
t.string "email"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "password_digest"
t.string "remember_digest"
t.string "activation_digest"
t.index ["email"], name: "index_users_on_email", unique: true end
end
Latest migration is
db/migrate/[timestamp]_add_activation_to_users.rb:
class AddActivationToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activation_digest, :string
add_column :users, :activated, :boolean, default: falserao
add_column :users, :activated_at, :datetime
end
end
The correct command to apply unapplied migrations is RAILS_ENV=development rake db:migrate
This just means that you have a migration pending. When you create a new migration
railas g migration MigrationName
it means that it changes something in the database schema or the layout of your database. In order to commit that change, you have to run:
bin/rails db:migrate
This will look into your migration file and apply it to your databse. Once the migration is done, you can run server as usual again.
rails server
If you have more question, I recommend reading the migration documentation that Rails published:
http://guides.rubyonrails.org/active_record_migrations.html
SQLite3::SQLException: duplicate column name: activation_digest:
(The following is the SQL command that actually modifies the db):
ALTER TABLE "users" ADD "activation_digest"
The SQL command reads like plain English. Somehow one of your migrations is doing something a previous migration already did, namely adding the activation_digest column to your users table in the db. If you look in the directory db/migrate/, you will see all your migration files. If you open one of them up, you should sort of be able to tell what it is doing. So look at all your migration files and find the two migrations that both add the activation_digest column. If the two migration files are identical, then you need to delete one--but take the following steps before deleting a migration:
https://www.baserails.com/questions/i-messed-up-while-generating-my-migration-how-can-i-undo-it
Also check out the section Rolling Back in the Rails Guide:
http://edgeguides.rubyonrails.org/active_record_migrations.html#reverting-previous-migrations
If for some reason you don't have two identical migration files that both add the activation_digest column, e.g one of the migration files does something additionally, then you need to figure out what steps in the tutorial that you did wrong, then rollback to the last migration that you know is correct. Finally, follow the steps in the tutorial again for generating the subsequent migrations.
It seems your users table already have a column named activation_digest. Remove the add_column line where the column is added from the migration file and run the migration again.
I think this migration file should work:
class AddActivationToUsers < ActiveRecord::Migration[5.0]
def change
add_column :users, :activated, :boolean, default: false
add_column :users, :activated_at, :datetime
end
end
Ok the answer is very simple!Just try migrating your DB to version=0 with command: rake db:migrate VERSION=0
and then run rake db:migrate

rake aborted database will not migrate

I created models, views, and controllers for 'startups' each individually (without scaffolding). I have a file db>migrate>'201..._create_startups.rb' with the code below:
class CreateStartups < ActiveRecord::Migration
def change
create_table :startups do |t|
t.string :name
t.string :location
t.string :description
t.timestamps null: false
end
end
end
I ran "bundle exec rake db:migrate" and I get this response:
== 20141126011749 CreateStartups: migrating ===================================
-- create_table(:startups)
-> 0.0155s
== 20141126011749 CreateStartups: migrated (0.0159s) ==========================
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
wrong number of arguments (1 for 0)/Users/kevinmircovich/.rvm/gems/ruby-2.0.0-p451/gems/activerecord-4.2.0.beta4/lib/active_record/connection_adapters/abstract_adapter.rb:271:in `initialize'
Once I run my local server and go to my browser to view my app, I have the message below:
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
Extracted source (around line #393):
392 def check_pending!(connection = Base.connection)
393 raise ActiveRecord::PendingMigrationError if ActiveRecord::Migrator.needs_migration?>.>(connection)
394 end
395
396 def load_schema_if_pending!
I ran "bin/rake db:migrate RAILS_ENV=development" and had the same error as I did when I ran "bundle exec rake db:migrate":
wrong number of arguments (1 for 0)
no need to "null: false" on timestamps: it is not users' input: those are set by the active model itself, so you can remove the argument.
In Rails migration t.timestamp macro adds two columns, created_at and updated_at. These special columns are automatically managed by Active Record if they exist.
It will automatically update when new recored created & updated.
Please remove null:false argument from t.timestamp.
class CreateStartups < ActiveRecord::Migration
def change
create_table :startups do |t|
t.string :name
t.string :location
t.string :description
t.timestamps
end
end
end
I received a similar error when running rake:db migrate. To resolve my issue I ran rake:db drop to drop my database since I was in dev mode with no production database. Then I recreate the database with rake db:create after which i ran rake db:migrate successfully.
Error running rake db:migrate
ActiveRecord::PendingMigrationError
Migrations are pending; run 'bin/rake db:migrate RAILS_ENV=development' to resolve this issue.
Resolved using:
rake db:drop - this will wipe the data out of your database
rake db:create
rake db:migrate

Ruby on Rails - error running server

am currently working on a rails project. When i tried to start rails server its throwing the following error:
=> Booting WEBrick
=> Rails 3.1.3 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
Exiting
/var/lib/gems/1.9.1/gems/activerecord-3.1.3/lib/active_record/connection_adapters
/sqlite_adapter.rb:439:in `table_structure': Could not find table 'dbrick'
(ActiveRecord::StatementInvalid)
My table name is 'dbrick'. I also Tried to rake db:drop and rake db:mirgrate. While migrating its throwing the following error:
rake aborted!
Could not find table 'dbrick'
Tasks: TOP => db:migrate => environment
(See full trace by running task with --trace)
This is my migrate file:
class CreateDbricks < ActiveRecord::Migration
def self.up
create_table :dbricks do |t|
t.text :description
t.string :video
t.string :video_html
t.string :image_id
t.string :option_id
t.boolean :choice
t.string :reach
t.integer :category_id
t.string :user_id
t.datetime :deleted_at
t.timestamps
end
end
def self.down
drop_table :dbricks
end
end
It will be so much help full if any one help me out of this.
Thanks in advance.
I would try :
rake db:schema:load
To load your schema ( to which I believe its finding the error against your DB ).
If that fails, I would manually find the migration that creates your dbrick, locate the name of the file and copy and paste the number in the filename to produce this :
rake db:migrate:down VERSION=123412341234 # <-- where the number is the number you pasted
Look for errors. Occasionally one thing exists already, or doesn't exist already and prevents the migration from running all the way, and consequentially that would be the source of your error. If it goes successfully then rake it back up :
rake db:migrate:up VERSION=123412341234 # <-- where the number is the number you pasted
If it doesn't go successfully, then you'll have to put on your miner's helmet, and get your hands dirty with :
rails dbconsole
Which will take you into your database and you'll have to manually delete whatever table/column is preventing the migration from occurring. Once that is fixed, exit out and rake db:migrate:up!
Have you migrated your database? rake db:migrate
If you have, drop your database (this deletes all data, so be careful - do it if you do not care about losing data in your db)
rake db:drop
This will clear out your database, and your schema. Then
rake db:migrate
This will re-migrate your schema.

$rake db:migrate An error has occurred, this and all later migrations canceled

I am new to RoR and I keep getting this error message:
$ rake db:migrate
== CreateUsers: migrating ====================================================
-- create_table(:users)
rake aborted!
An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "users" already exists: CREATE TABLE "users" ("id"
INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "name" varchar(255), "email" varchar
(255), "created_at" datetime, "updated_at" datetime)
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I've been searching for a solution for 3 days, but I cannot seem to find anything that works for me.
Thank you in advance for your help :)
PS - I am running off Windows.
Not sure if you are following Michael Hartl's tutorial on RoR.
But someone has said there's a problem in the steps of the tutorial http://archive.railsforum.com/viewtopic.php?id=44944
rake db:drop:all <---------- will wipe everything then run rake db:migrate again should fix the problem.
Good Luck
table "users" already exists seems to be the problem. Have you tried to manually remove the table from your database with some SQLITE admin tool?
Or you can include a remove table in your migration script (should be called create_users.rb inside your db/migrate folder). Inside def up insert drop_table :users :
def up
drop_table :users
create_table :users do |t|
t.string :name
#...
t.timestamps
end
Oh and I remember from my RoR time that the table name "Users" can cause problems later on. Might be this is related.
Because the table already exists, you need to delete/remove it before executing the migration.
Easy, GUI way to do this is with the SQLite Database Browser (http://sourceforge.net/projects/sqlitebrowser/).
Click the button with the Table-X icon. Choose User Table click Delete.
Then run rake db:migrate
Bada boom bada bing
I had the same problem and after several hours I finally found the solution
I’ve put
def self.up
create_table :users do |t|
def down
drop_down :users
end
end
Then make rake db:migrate and Magic !!!!
I had a similar problem, then i did
=> rake db:drop
=> rake db:create
=> rake db:migrate
worked perfectly.
But if it doesn't work we could try something like
ActiveRecord::Migration.drop_table('users')
ActiveRecord::Migration.create_table('users')

Resources