Reputation: 1787
i'm new to rails and following a tutorial where we created some entries. Currently when I run the server it tells me
Migrations are pending. To resolve this issue, run: bin/rake db:migrate RAILS_ENV=development
Then I noticed after seeing some SO posts, I should do things like run
rake db:migrate
Then I realized when I did that I got a bunch of lines and one which had this
ActiveRecord::StatementInvalid: SQLite3::SQLException: duplicate column name: description: ALTER TABLE "articles" ADD "description" text
Which I presume the problem comes from. Now i'm not an expert in rails, but how can I identify where this duplicate column is from, when I do
rails console- > Article.all
I get
<ActiveRecord::Relation [#<Article id: 1, title: "This is my first article",
description: "This is the description", created_at: "2016-07-20 00:03:42",
updated_at: "2016-07-20 00:03:42">, #<Article id: 2, title: "This is an endited
title", description: "This is my second description", created_at: "2016-07-20
00:05:39", updated_at: "2016-07-20 00:40:54">]
Which I created yesterday. But where is this duplicate description field? Where should I be looking?
My two migration files look like this by the way
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :description
t.timestamps null: false
end
end
end
Another
class AddDescriptionToArticles < ActiveRecord::Migration
def change
add_column :articles, :description, :text
add_column :articles, :created_at, :datetime
add_column :articles, :updated_at, :datetime
end
end
My schema
ActiveRecord::Schema.define(version: 20160715020218) do
create_table "articles", force: :cascade do |t|
t.string "title"
t.text "description"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
end
Upvotes: 0
Views: 2465
Reputation: 1428
The reason you are getting the error saying you have duplicate columns is because you simply are trying to add columns which already exist in your model.
lets run through your first migration file:
class CreateArticles < ActiveRecord::Migration
def change
create_table :articles do |t|
t.string :title
t.text :description
t.timestamps null: false
end
end
end
In the create_table block you have the following lines:
t.string :title
<- this creates a title column of type string.
t.text :description
<- this creates a description column of type text.
t.timestamps, null: false
<- this creates two columns: created_at
and updated_at
both of type datetime with the not_null flag on them.
Now in your second migration file you are trying to add three columns:
add_column :articles, :description, :text
add_column :articles, :created_at, :datetime
add_column :articles, :updated_at, :datetime
But as we stated above they have already been created, and therefore are duplicates which can not be made. So the best option to solve your problem would be to just delete this second migration file where you are using add_column and everything should be ok.
Upvotes: 1
Reputation: 1374
in the second migration take out the line add_column :articles, :description, :text
. You already created a description
column when you created the articles
table in the first migration, with the line t.text :description
:
create_table :articles do |t|
t.string :title
t.text :description
t.timestamps null: false
end
That's the duplicate column Rails is referring to in the error. Next time before you run migrations you can check schema.rb
file to see what your database tables look like
Missed this the first time, but you also are duplicating your timestamp columns, created_at
and updated_at
So the second migration is unnecessary
Upvotes: 1