Matt Lewis
Matt Lewis

Reputation: 21

Why is my Rails DB migration not creating columns?

I'm trying to do a DB migration in Rails using PostgreSQL, but the resulting schema doesn't contain any of my table definitions. Is there something wrong with my syntax that I'm not seeing?

Here's an example of one of my migration files and the resulting schema file after I run "rake db:migrate".

Migration file:

class Fields < ActiveRecord::Migration[5.2]

      def change

        def up



          create_table :fields do |t|

            t.column :totalsalesprsn, :float, :limit => nil, :null => false

            t.column :totaladmkspend, :float, :limit => nil, :null => false

            t.column :totalsalescost, :float, :limit => nil, :null => false

            t.column :miscsales, :float, :limit => nil, :null => false

            t.column :numleads, :float, :limit => nil, :null => false

            t.column :costleads, :float, :limit => nil, :null => false

            t.column :totalsalescost2, :float, :limit => nil, :null => false

            t.column :totalmarketspent, :float, :limit => nil, :null => false

            t.column :numsales, :float, :limit => nil, :null => false

            t.column :averagecost, :float, :limit => nil, :null => false

            t.column :costpersale, :float, :limit => nil, :null => false

            t.column :totalspending, :float, :limit => nil, :null => false

            t.column :totalsalesdonate, :float, :limit => nil, :null => false

            t.column :totalsales, :float, :limit => nil, :null => false

            t.column :pototal, :float, :limit => nil, :null => false

            t.column :posales, :float, :limit => nil, :null => false



            t.column :form_id, :integer

            t.column :created_at, :timestamp

          end

        end



        def down

          drop_table :fields

        end

      end

    end

Schema file:

ActiveRecord::Schema.define(version: 2018_10_25_161515) do



  # These are extensions that must be enabled in order to support this database

  enable_extension "plpgsql"



  create_table "fields", force: :cascade do |t|

    t.datetime "created_at", null: false

    t.datetime "updated_at", null: false

  end



  create_table "forms", force: :cascade do |t|

    t.datetime "created_at", null: false

    t.datetime "updated_at", null: false

  end



  create_table "tables", force: :cascade do |t|

    t.datetime "created_at", null: false

    t.datetime "updated_at", null: false

  end



end

Would it have something to do with my model files? I have no idea why it's doing this, and I can't post more code, because I would have to add more details to this post in order to avoid the warning that my question doesn't have enough details.

Upvotes: 1

Views: 1254

Answers (3)

EmmanuelB
EmmanuelB

Reputation: 1427

you have your syntax somewhat wrong:

class Fields < ActiveRecord::Migration[5.2]
  def change
    def up
     # your table definition
    end
  end

  def down
    # delete your table
  end
end

Rails is smart enough to know that the reverse of creating a table is deleting it, so you don't need to specify def down.

You don't seem to show errors on the migration, so before doing the following, run the command rails db:rollback from your console

Then change your migration file to the following and run again rails db:migrate:

class Fields < ActiveRecord::Migration[5.2]
  def change
   # your table definition
  end
end

Upvotes: 2

lacostenycoder
lacostenycoder

Reputation: 11196

Other answers here are correct. However if you already ran the migration and got no error, you will likely need to roll it back, but if that fails you will need to manually rollback the migration in the database. Because any migration will increment the schema_migrations with the version number in the migration filename.

So if your migration filename is something like 20181023191125_fields.rb you'll need to do this:

rails dbconsole
#now you should be in a (pg?) console
DELETE from schema_migrations WHERE version = 20181023191125;
\q # to quit postgres

But if this is a new project and you have seeds, it might be easier to just start over with a fresh database after fixing your migration as others have already instructed you.

ONLY DO THIS IF YOU ARE FINE WITH KILLING YOUR DB and STARTING OVER!!

rake db:setup
# or
rails db:setup

See Difference between rake db:migrate db:reset and db:schema:load

Upvotes: 0

user3723506
user3723506

Reputation:

It's not creating columns because you are defining up and down inside the change method.

Try this

class Fields < ActiveRecord::Migration[5.2]
  def up
    create_table :fields do |t|
      t.column :totalsalesprsn, :float, :limit => nil, :null => false
      t.column :totaladmkspend, :float, :limit => nil, :null => false
      t.column :totalsalescost, :float, :limit => nil, :null => false
      t.column :miscsales, :float, :limit => nil, :null => false
      t.column :numleads, :float, :limit => nil, :null => false
      t.column :costleads, :float, :limit => nil, :null => false
      t.column :totalsalescost2, :float, :limit => nil, :null => false
      t.column :totalmarketspent, :float, :limit => nil, :null => false
      t.column :numsales, :float, :limit => nil, :null => false
      t.column :averagecost, :float, :limit => nil, :null => false
      t.column :costpersale, :float, :limit => nil, :null => false
      t.column :totalspending, :float, :limit => nil, :null => false
      t.column :totalsalesdonate, :float, :limit => nil, :null => false
      t.column :totalsales, :float, :limit => nil, :null => false
      t.column :pototal, :float, :limit => nil, :null => false
      t.column :posales, :float, :limit => nil, :null => false
      t.column :form_id, :integer
      t.column :created_at, :timestamp
    end
  end

  def down
    drop_table :fields
  end
end

Please have a look at the documentation here on how to define migrations.


From the documentation

The change method is the primary way of writing migrations. It works for the majority of cases, where Active Record knows how to reverse the migration automatically.

So, alternatively you can define the migration as well by doing the following

class Fields < ActiveRecord::Migration[5.2]
  def change
    create_table :fields do |t|
      t.column :totalsalesprsn, :float, :limit => nil, :null => false
      t.column :totaladmkspend, :float, :limit => nil, :null => false
      t.column :totalsalescost, :float, :limit => nil, :null => false
      t.column :miscsales, :float, :limit => nil, :null => false
      t.column :numleads, :float, :limit => nil, :null => false
      t.column :costleads, :float, :limit => nil, :null => false
      t.column :totalsalescost2, :float, :limit => nil, :null => false
      t.column :totalmarketspent, :float, :limit => nil, :null => false
      t.column :numsales, :float, :limit => nil, :null => false
      t.column :averagecost, :float, :limit => nil, :null => false
      t.column :costpersale, :float, :limit => nil, :null => false
      t.column :totalspending, :float, :limit => nil, :null => false
      t.column :totalsalesdonate, :float, :limit => nil, :null => false
      t.column :totalsales, :float, :limit => nil, :null => false
      t.column :pototal, :float, :limit => nil, :null => false
      t.column :posales, :float, :limit => nil, :null => false
      t.column :form_id, :integer
      t.column :created_at, :timestamp
    end
  end
end

Upvotes: 6

Related Questions