I am trying to run rake db:migrate and am receiving an error in the console.
It seems as though I am creating a table that already exists, yet I don't know how to remove the old table, or reset the db to start fresh.
I don't have any users so erasing or starting from fresh won't be an issue.
create_table(:users) rake aborted! StandardError: 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, "email"
varchar(255) DEFAULT '' NOT NULL, "encrypted_password" varchar(255)
DEFAULT '' NOT NULL, "reset_password_token" varchar(255),
"reset_password_sent_at" datetime, "remember_created_at" datetime,
"sign_in_count" integer DEFAULT 0 NOT NULL, "current_sign_in_at"
datetime, "last_sign_in_at" datetime, "current_sign_in_ip"
varchar(255), "last_sign_in_ip" varchar(255), "created_at" datetime,
"updated_at" datetime)
/Users/jovanhernandez/.rvm/gems/ruby-2.1.2/gems/sqlite3-1.3.9/lib/sqlite3/database.rb:91:in
`initialize'
If you don't mind erasing data you can run
rake db:drop
rake db:create
rake db:migrate
and that should fix it. Otherwise you can for the moment comment out the part of the content causing problems in the change (or up) method in your migration and then run the migrations. After the migration is run uncomment the migration.
Doing this tricks rails into accepting that the migrations are up to date.
You can reset database by using
$ rake db:reset
Or if you want to undo your migration you can use
$ rake db:rollback
Or if you want to update your table, you may change it in your migration file from create_table to change_table
create_table :users do |t|
to
change_table(:users) do |t|
Look like you have not any users migration but you have migration to change the users table.Means you are trying to alter users table without existence of it.If you have schema file then you can load your database from there by using rake db:schema:dump command.
You already have the users table created. Looks like the migration was already run and there is no need to run it again.
Related
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.
when I run this command bin/rake db:migrate
I get this error
== 20151020021106 CreateTodoItems: migrating ================================== -- create_table(:todo_items) rake aborted! StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "todo_items" already exists: CREATE TABLE "todo_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todo_list_id" integer, "content" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /home/youta/rails_projects/odot/db/migrate/20151020021106_create_todo_items.rb:3:in change' -e:1:in' ActiveRecord::StatementInvalid: SQLite3::SQLException: table "todo_items" already exists: CREATE TABLE "todo_items" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "todo_list_id" integer, "content" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) /home/youta/rails_projects/odot/db/migrate/20151020021106_create_todo_items.rb:3:in change' -e:1:in' SQLite3::SQLException: table "todo_items" already exists /home/youta/rails_projects/odot/db/migrate/20151020021106_create_todo_items.rb:3:in change' -e:1:in' Tasks: TOP => db:migrate (See full trace by running task with --trace)
To solve it, I tried to remove the model todo_item by using these commands
rake db:rollback
rails destroy model todo_item
rake db:drop
rake db:setup
rake db:drop RAILS_ENV=test
rake db:setup RAILS_ENV=test
Although when I tried to remake the todo_item model after removing the previous one I still get the same error!
should I reset git .. if so, how can I reset it to the previous working version
Running rake db:setup is not the same as running all migrations, but uses your schema.rb to rebuild the database, which still includes the todo_items table.
Drop the database again with rake db:drop, and run rake db:migrate to only use the migrations in your migrate directory.
I am new to both Ruby and Rails. So this may be an easy fix. I'm sorry if it is.
I recently installed Ruby-on-Rails and started following the tutorial on rubyonrails.org which shows how to make a simple blog. Everything was running fine until I got to section 5.5. I went to run db:migrate and it gave me an error.
|D:\Documents\Programs\Ruby\blog>rake db:migrate
== 20141216061542 CreateArticles: migrating ===================================
-- create_table(:articles)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "articles" already exists: CREATE TABLE "articles" ("id" INTEGER
PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "text" text, "created_at" datetime,
"updated_at"
datetime) D:/Documents/Programs/Ruby/blog/db/migrate/20141216061542_create_articles.rb:3:in
`change
'
C:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "articles" already exists: CREATE
TABLE
"articles" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "title" varchar(255), "text" text,
"created_at" datetime, "updated_at" datetime)
D:/Documents/Programs/Ruby/blog/db/migrate/20141216061542_create_articles.rb:3:in `change'
C:in `migrate'
SQLite3::SQLException: table "articles" already exists
D:/Documents/Programs/Ruby/blog/db/migrate/20141216061542_create_articles.rb:3:in `change'
C:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
I fired up the server to see what it would show and it gave me this:
ActiveRecord::PendingMigrationError
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
It's been doing this ever since. I have tried starting over by deleting the project.(not entirely sure if that was a good move.) I have tried looking over the code. Nothing I have tried has given me any hints on what to do.
Is there any way to get rid of these errors?
Thank you in advance.
EDIT:
I tried to reset the database with 'rake db:reset', but it just gave me this:
|D:\Documents\Programs\Ruby\blog\app\views\articles>rake db:reset
(in D:/Documents/Programs/Ruby/blog)
Permission denied # unlink_internal - D:/Documents/Programs/Ruby/blog/db/development.sqlite3
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/fileutils.rb:1460:in `unlink'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/fileutils.rb:1460:in `block in remove_file'
C:/RailsInstaller/Ruby2.1.0/lib/ruby/2.1.0/fileutils.rb:1468:in `platform_support'
...
rake aborted!
Errno::EACCES: Permission denied # unlink_internal -
D:/Documents/Programs/Ruby/blog/db/development.
sqlite3
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)
I shortened it for readability.
And here is my create_articles migration file:
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.timestamps
end
end
end
You've already created that particular table. Try this from your terminal:
rake db:drop db:create db:migrate
Or:
rake db:reset db:migrate
So basically, you will start your database from scratch, which will avoid the current error.
Note that for new migrations, you only run the 'rake db:migrate' command otherwise your existing data will be lost.
Later on if you come across this problem in a production environment, ensure that you do 'something else' - surely you wouldn't want to sacrifice your production database data.
Well It seems obvious, you already have table articles, and you are trying to create a new one.
Two options:
comment migration with articles do rake db:migrate, uncomment for other environment (if any)
clear database and run migrations again.
Add create_articles to your question as well, could help resolving the problem.
Drop the db
rake db:drop
And Migrated it once again
rake db:migrate
You have already create articles tables. So you need to drop it and migrate it once again.
I'm working on a Rails 3 app and am trying to run a migration. I am trying to create a table called Songs and I was able to successfully create the table and migrate locally. However, when I push the code to Heroku and migrate there, the error I encounter is:
** Execute db:migrate
== CreateSongs: migrating ====================================================
-- create_table(:songs)
NOTICE: CREATE TABLE will create implicit sequence "songs_id_seq1" for serial column "songs.id"
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "songs" already exists
: CREATE TABLE "songs" ("id" serial primary key, "solo_cello" character varying(255), "created_at" timestamp, "updated_at" timestamp) /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.12/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `async_exec'
I realize this error stems from the table already existing in the database, however when I run
heroku run "bundle exec rake db:schema:dump && cat db/schema.rb"
I see that the schema version is at ActiveRecord::Schema.define(:version => 20130915155113) and the migration to create the Songs model occurs after this migration (it is 20140506043817).
Here's is a truncated version of my local schema:
ActiveRecord::Schema.define(:version => 20140511155456) do
[other tables...]
create_table "songs", :force => true do |t|
t.string "solo_cello"
t.datetime "created_at"
t.datetime "updated_at"
t.string "solo_violin"
t.string "duo_one"
t.string "duo_two"
t.string "trio_a_one"
t.string "trio_a_two"
t.string "trio_a_three"
t.string "trio_b_one"
t.string "trio_b_two"
t.string "trio_b_three"
t.string "quartet_one"
t.string "quartet_two"
t.string "quartet_three"
t.string "quartet_four"
end
Here are my questions:
How does the table already exist if the schema on the database is at version 20130915155113 if the migration that creates the Song table (20140506043817) comes later?
What do I need to do to run a successful migration? I realize I can drop the database (this is the staging environment) but would greatly prefer not to do that on production.
Why do migrations work fine on my machine locally but not on Heroku?
Sounds like your songs table might have been left behind as effect of restoring a database backup. Restoring a database backup on Heroku will only rewrite tables which exist in the backup, but it won't drop tables which didn't yet exist when the backup was created.
There are several steps to follow to resolve the issue:
Drop the table. You can manually drop the table by running heroku pg:psql in the terminal, and then executing the query DROP TABLE songs;. If this was the only problematic table, you can now run your migrations.
Drop the database. In case you have a database backup and you can afford some downtime, you can also drop the database by running heroku pg:reset from the terminal, then restoring your database backup and running your migrations.
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')