-1

Ok so you've built your model 'rails generate model test (inputs)' you've rake db:migrate checked your schema file all looks good and working and writing to sqlite. But then you realise you need another attribute in the DB. One way is to rake db:rollback, make change in the migrate file, rake db:migrate again and hey presto all looking good. However doing rake db:rollback has lost all my data already stored in the sqlite. So I'm guessing this is not the correct way to do this? What is the best way?

class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
  t.text :title
  t.text :requester
  t.text :requester_email
  t.text :customer
  t.text :contact
  t.text :customer_email
  t.text :customer_phone
  t.string :type_of_change
user3216968
  • 85
  • 1
  • 6
  • Are you looking for this? http://stackoverflow.com/questions/10352832/adding-a-new-field-to-rails-model/ – Matzi Apr 19 '14 at 13:30
  • yeah I think this is where I should be looking except I'm trying to add new tables instead of columns – user3216968 Apr 19 '14 at 13:38
  • What do you mean by `adding new tables` with the scenario given on the question? Explicitly you have said that you tried to add an attribute/column to an already added migration. The above link gives you the perfect answer for that. – Rajesh Omanakuttan Apr 19 '14 at 13:40
  • possible duplicate of [Adding a column to a table in rails](http://stackoverflow.com/questions/4834809/adding-a-column-to-a-table-in-rails) – Rajesh Omanakuttan Apr 19 '14 at 13:51
  • ok so above is my migration file. I want to add a new item to it through the migration command. the command rails g migrate CreatePosts implementer:text tells me the file already exists which is does. I know that... I just want to add to it in the correct way without loosing data from the DB. – user3216968 Apr 19 '14 at 13:52

3 Answers3

1

You can generate a new migration with rails g migration <migration_name> (eg. rails g migration add_foo_to_blah) and then inside that new migration, make the changes you need.

eg. you can add new columns, rename columns, etc.

For more information: http://guides.rubyonrails.org/migrations.html#creating-a-standalone-migration

sevenseacat
  • 24,699
  • 6
  • 63
  • 88
0

Yes, db:rollback takes back the last migration, so if in last migration you added some columns, db:rollback will remove them (and if you change the migration and run it again, these fields will be added, but data is already lost).

The proper way to add more fields is to generate new migration and add these fields there.

Gregory Witek
  • 1,216
  • 8
  • 10
0

In fact db:rollback is best way. Of course when you do db:rollback it will wipe away your existing data,but you need to do db:migrate to get the data appear again in the tables.

If you don't want to do rake db:rollback,then create another migration file which will do the job for you.

Edit

You need to perform the below command to get that column added in your posts table.

rails g migration AddImplementerToPosts implementer:text

and do rake db:migrate to make sure that column added in your table.

Pavan
  • 33,316
  • 7
  • 50
  • 76
  • Ok but I find that the data stored in the database that users have input has been lost too. It does restore the tables for sure but not the input data. – user3216968 Apr 19 '14 at 13:54
  • ok I've done that... I would expect to see a new addition like the above example that reads t.text:implementer but in a new file of course. However I don't I see this... add_column :posts, :implementer, :text – user3216968 Apr 19 '14 at 14:02
  • rails g migration AddColumnToPosts implementer:text – user3216968 Apr 19 '14 at 14:06
  • @user3216968 Try with this command `rails g migration AddImplementerToPosts implementer:text` – Pavan Apr 19 '14 at 14:08
  • I get this SQLite3::SQLException: duplicate column name: implementer: – user3216968 Apr 19 '14 at 14:12
  • @user3216968 That means you have already a column named `implementer` in your `posts` table.Please check the table. – Pavan Apr 19 '14 at 14:14
  • @user3216968 Try with this `ActiveRecord::Base.connection.table_structure("posts")` to get the table structure for `posts` – Pavan Apr 19 '14 at 14:17
  • ok if I just type 'Post' on the console that gives me all the tables. which looks like it's working ok from the first command you gave me. However looking at the migration files it just doesn't look clean or easy to follow. – user3216968 Apr 19 '14 at 14:22
  • @user3216968 Make sure next time you generate the right migration files.If you are satisfied with my answer,plese accept it. – Pavan Apr 19 '14 at 14:25