Search code examples
ruby-on-railsrubyrakedbmigrate

Ruby-on-Rails Tutorial Trouble


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

Solution

  • 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.