Search code examples
ruby-on-railspostgresqldatabase-migration

rails migration generating random tables. Where is this coming from?


When I run rails db:migrate without a new migration it seems to have added two new tables: questions and questions_1.

In my schema file I see:

  create_table "questions", id: false, force: :cascade do |t|
    t.integer "id"
    t.text "text"
    t.boolean "active"
    t.integer "organization_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.bigint "account_id"
    t.bigint "team_id"
  end

  create_table "questions_1", id: false, force: :cascade do |t|
    t.integer "id"
    t.text "text"
    t.boolean "active"
    t.integer "organization_id"
    t.datetime "created_at"
    t.datetime "updated_at"
    t.bigint "account_id"
    t.bigint "team_id"
  end

I don't have any migrations making these tables. I'm guessing this is some sort of convention. Where do I look to fix this? All recent changes are in the app/ directory and a migration I made has been rolled back and then deleted. Yet when I run rails db:migrate I always get these new tables.

Any ideas?


Solution

  • Rails won't create tables with any name other than the ones specified in a migration. If the table already exists then it will throw an error.

    I believe someone has run a migration creating the second table on your database, and then deleted the migration. When rails creates your schema.rb file, it uses your database rather than any migrations, meaning this file reflects your actual database state rather than the one your migrations suggest would be the case.

    If you're sure you don't want the second table and it has no data in it, then you can write out a new migration to delete this table, run it, and then delete it. This will return your database state to the one your migrations suggest would be the case. You can then run rake db:schema:dump to update your schema.