0

This is probably really simple/obvious but I am new to this -

I have a database with some tables, let's say tA, tB and tC. In a migration I dropped tC, because at that point I needed to working on a different branch. However, I'm now working on a branch in a project that needs tC to exist.

The migrations for creating all tables are present on this branch.

I don't care if tC is empty. But if I run rake db:create, will it then re-create all tables? Because I do not want to lose the data in the other tables, tA & tB.

TL;DR - How to create a table (which I dropped in an earlier creation) without also recreating (and losing the data of) the tables I did not drop?

Teresa
  • 193
  • 1
  • 2
  • 20
  • 1
    If you dropped tC in a migration in a `change` block you can simply `rake db:rollback` to re-create that table. – Ho Man Mar 28 '17 at 09:43

2 Answers2

1

How did you drop the table tC? Is it through console/command line? If yes, then to recreate the tC table, you have 2 choices:

  1. Create a new migration file to only create table tC (optimal and easiest fix)

    bundle exec rails g migration CreateTableTC
    

Then add the new migration with this conditional statement:

    unless table_exists?(:table_tC)
      create_table :table_tC do |t|
        ........
      end
    end

Then run this command on your terminal:

    bundle exec rake db:migrate

This will create table tC if your database hasn't got one, will not erase data on other tables, and will not be executed if your database has table tC

  1. Use GUI for your database (if you are using postgres, you may use PgAdmin)
Galih Muhammad
  • 166
  • 1
  • 5
0

Here's what I ended up doing: using the Rails console, I simply ran the migration that created that particular table in the first place again. See this Q&A: Run a single migration file.

Community
  • 1
  • 1
Teresa
  • 193
  • 1
  • 2
  • 20