Ricardo Pérez
2006-Dec-20 17:31 UTC
Problem with add_column in migrations with PostgreSQL
Hello everyone.
I''m a Rails newbie, and I have a question about migrations with
PostgreSQL.
I''m using the svn version of Rails, in this way:
~ $ rails depot
~ $ cd depot
~/depot $ rake rails:freeze:edge
The application will use a PostgreSQL database, so I configure my app
accordingly editing the config/database.yml file.
Now I create a model for the items table:
~/depot $ ruby script/generate model item
It creates the file db/migrate/001_create_items.rb, which I modify until
I get the following contents:
class CreateItems < ActiveRecord::Migration
def self.up
create_table :items do |t|
t.column :descr, :string
t.column :price, :decimal, :precision => 5, :scale => 2
end
end
def self.down
drop_table :items
end
end
Finally, I do:
~/depot $ rake db:migrate
which creates the items table correctly:
depot_development=# \d items
Table "public.items"
Column | Type |
Modifiers
--------------+------------------------+--------------------------------------------------------
id | integer | not null default
nextval(''items_id_seq''::regclass)
descr | character varying(255) |
price | numeric(5,2) |
Indexes:
"items_pkey" PRIMARY KEY, btree (id)
Now it comes the problem:
I want to add a new column, so therefore I create a new migration:
~/depot $ ruby script/generate migration add_cost
I modify the file db/migrate/002_add_cost.rb, which I modify so it looks
like this:
class AddCost < ActiveRecord::Migration
def self.up
add_column :items, :cost, :decimal, :precision => 6, :scale => 2
end
def self.down
remove_column :items, :cost
end
end
Then I do:
~/depot $ rake db:migrate
which creates the new column, BUT WITHOUT PRECISION NOR SCALE:
depot_development=# \d items
Table "public.items"
Column | Type |
Modifiers
--------------+------------------------+--------------------------------------------------------
id | integer | not null default
nextval(''items_id_seq''::regclass)
descr | character varying(255) |
price | numeric(5,2) |
cost | numeric |
Indexes:
"articulos_pkey" PRIMARY KEY, btree (id)
Look at the difference between price and cost columns. "price" has
precision (5) and scale (2), but cost is only "numeric", without
precision or scale. However, I specify {:precision => 6, :scale => 2} in
the add_column command.
It looks like an add_column bug. What do you think?
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---