0

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
hmmm
  • 13
  • 5

3 Answers3

2

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.

SHS
  • 7,651
  • 3
  • 18
  • 28
  • you can simply do: `rake db:drop db:create db:migrate` or `rake db:reset db:migrate`. No need to reload rake every task – davegson Dec 16 '14 at 14:01
  • Ok +1 for the solution. You should probably explain why this will help him. Because it sure isn't a solution for a production environment :) – davegson Dec 16 '14 at 14:05
  • 1
    @SyedHumzaShah actually rake db:reset will be enough. It does db:migrate by itself – kirqe Dec 16 '14 at 14:05
  • @railsr, I think db:reset loads the existing schema - it doesn't run pending migrations. – SHS Dec 16 '14 at 14:07
  • 1
    @railsr, true, check http://stackoverflow.com/questions/10301794/difference-between-rake-dbmigrate-dbreset-and-dbschemaload – davegson Dec 16 '14 at 14:09
  • I tried using 'rake db:reset', but it gave me a new error. I'll edit it in the question. – hmmm Dec 16 '14 at 14:10
0

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.

Miknash
  • 7,888
  • 3
  • 34
  • 46
0

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.

Amrit Dhungana
  • 4,371
  • 5
  • 31
  • 36