Table already exists error

313 views
Skip to first unread message

eye0rama

unread,
Nov 16, 2016, 1:53:52 AM11/16/16
to Ruby on Rails: Talk

Hi all,

I have been learning Rails by working through Carneiro and Barazi's *Beginning Rails 3*. This has went well despite the fact that I am using Rails 4.2.6. I hit a little snag when in chapter 5, which goes over associations, I tried to create a has_many association between a user and that user's articles, specifically in the code that is supposed to sort the user's articles first by order of creation, and then by alphabetical order. I fixed this problem, with a little help from the Ruby Talk mailing list, with a lambda scope block, as follows in app/models/user.rb:


class User < ActiveRecord::Base                                        
     has_one :profile                                               
     has_many :articles, -> { order 'published_at DESC, title ASC' } :dependent => :nullify
end   


This seemed to work, so I went through the steps listed again, taking a stab that I could overwrite everything rather than just starting over.  When I ran rake db:migrate, I got the following error:

== 20161114230702 CreateArticlesCategories: migrating =========================
-- create_table(:articles_categories, {:id=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "articles_categories" already exists: CREATE TABLE "articles_categories" ("article_id" integer, "category_id" integer)
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "articles_categories" already exists: CREATE TABLE "articles_categories" ("article_id" integer, "category_id" integer)
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'
SQLite3::SQLException: table "articles_categories" already exists
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

I did some poking around in the rails console, and running: category.articles.empty?

Returned: NameError: undefined local variable or method `category' for main:Object

Is there a way I can fix this without just deleting everything and starting over?

Colin Law

unread,
Nov 16, 2016, 3:32:44 AM11/16/16
to Ruby on Rails: Talk
On 16 November 2016 at 01:46, eye0rama <welcome.to...@gmail.com> wrote:

Hi all,

I have been learning Rails by working through Carneiro and Barazi's *Beginning Rails 3*. This has went well despite the fact that I am using Rails 4.2.6. I hit a little snag when in chapter 5, which goes over associations, I tried to create a has_many association between a user and that user's articles, specifically in the code that is supposed to sort the user's articles first by order of creation, and then by alphabetical order. I fixed this problem, with a little help from the Ruby Talk mailing list, with a lambda scope block, as follows in app/models/user.rb:


class User < ActiveRecord::Base                                        
     has_one :profile                                               
     has_many :articles, -> { order 'published_at DESC, title ASC' } :dependent => :nullify
end   


This seemed to work, so I went through the steps listed again, taking a stab that I could overwrite everything rather than just starting over.  When I ran rake db:migrate, I got the following error:

== 20161114230702 CreateArticlesCategories: migrating =========================
-- create_table(:articles_categories, {:id=>false})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "articles_categories" already exists: CREATE TABLE
"articles_categories" ("article_id" integer, "category_id" integer)
/home/john/src/ror/blog/db/migrate/20161114230702_create_articles_categories.rb:3:in `change'


You are trying to run a migration (20161114230702_create_articles_categories.rb) that adds that table, but the table already exists in the database.  Have you added a second migration that tries to add it again? If it is not that then the easiest solution may be to delete and recreate the db again.

Colin

Edsil Basadre

unread,
Nov 16, 2016, 6:22:15 AM11/16/16
to rubyonra...@googlegroups.com
Try to comment out the method that creating the table in migration 20161114230702_create_articles_categories.rb

Colin Law

unread,
Nov 16, 2016, 6:40:29 AM11/16/16
to Ruby on Rails: Talk
On 16 November 2016 at 11:21, Edsil Basadre <eebas...@gmail.com> wrote:
Try to comment out the method that creating the table in migration 20161114230702_create_articles_categories.rb

That is not really appropriate unless the OP understands why this has happened.

Colin

Edsil Basadre

unread,
Nov 17, 2016, 7:02:40 AM11/17/16
to rubyonra...@googlegroups.com
Thanks for correcting me Colin. :)

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rubyonrails-talk+unsubscribe@googlegroups.com.
To post to this group, send email to rubyonrails-talk@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAL%3D0gLsGayWOQbhLnydokjxmznc_PyU_Lwt0Q3CZ0_OzfmRmJw%40mail.gmail.com.

For more options, visit https://groups.google.com/d/optout.

eye0rama

unread,
Nov 19, 2016, 9:10:21 AM11/19/16
to Ruby on Rails: Talk

I did not have a second migration trying to add it again, so I ran:

rake db:drop db:create db:migrate

I was able to do everything after that.

Thank you for the assistance!
Reply all
Reply to author
Forward
0 new messages