Melbourne2991
Melbourne2991

Reputation: 11797

db:migrate table not being created

I am trying to create a table with 3 columns url, price and timestamps...I tried both "change" and "up"

class Shp < ActiveRecord::Base
  def change
    create_table :shps do |t|
      t.string :url
      t.float :price
      t.timestamps
    end
  end
end

Running db:migrate seems to do nothing, as when I do

ActiveRecord::Base.connection.column_names("shps")

I get a table with default columns only.

=> [#<ActiveRecord::ConnectionAdapters::SQLiteColumn:0x8fc8a34 @name="id", @sql_type="INTEGER", @null=false, @limit=nil, @precision=nil, @scale=nil, @type=:integer, @default=nil, @primary=nil, @coder=nil>, #<ActiveRecord::ConnectionAdapters::SQLiteColumn:0x8fc878c @name="created_at", @sql_type="datetime", @null=false, @limit=nil, @precision=nil, @scale=nil, @type=:datetime, @default=nil, @primary=nil, @coder=nil>, #<ActiveRecord::ConnectionAdapters::SQLiteColumn:0x8fc8444 @name="updated_at", @sql_type="datetime", @null=false, @limit=nil, @precision=nil, @scale=nil, @type=:datetime, @default=nil, @primary=nil, @coder=nil>]

Upvotes: 0

Views: 86

Answers (2)

a.s.t.r.o
a.s.t.r.o

Reputation: 3338

This should work:

app/models/shp.rb:

class Shp < ActiveRecord::Base
  set_table_name "shps"
end

db/migrate/2013xxxxxxxxx_create_shps.rb:

class CreateShps < ActiveRecord::Migration
  def change
    create_table(:shps) do |t|
      t.string :url
      t.float :price
      t.timestamps
    end
  end
end

HTH

Upvotes: 1

Marek Lipka
Marek Lipka

Reputation: 51151

Migration classes should inherit from ActiveRecord::Migration instead of ActiveRecord::Base. They should also be placed in proper directory (db/migrate) and their filename should contain appropriate timestamp.

To generate migration among with model, you should type in your console:

rails g model Shp url:string price:float

and run migration:

bundle exec rake db:migrate

BTW for your own convenience try to make class names more descriptive.

Upvotes: 1

Related Questions