I'm trying to add a new column, 'latitude', to an existing Postgres table, after the 'location' column.
Using this syntax puts the column in the correct place:
add_column :table, :column, :decimal, :after => :existing_column
And using this syntax ensures that the field is the correct data type
add_column :table, :column, :decimal, {:precision => 10, :scale => 6}
But when I try and combine the two:
add_column :table, :column, :decimal, {:precision => 10, :scale => 6}, :after => :existing_column
I get "ArgumentError: wrong number of arguments (5 for 3..4)"
"Not to worry", I thought, "I'll just combine the arguements!":
add_column :table, :column, :decimal, {:precision => 10, :scale => 6, :after => :existing_column}
But then the columns appear at the end of the table. What am I doing wrong?
Thanks :)
When you already have users and uploads tables and wish to add a new relationship between them. Then, run the migration using rake db:migrate . This migration will take care of adding a new column named user_id to uploads table (referencing id column in users table), PLUS it will also add an index on the new column.
You must rollback the migration (for example with bin/rails db:rollback ), edit your migration, and then run bin/rails db:migrate to run the corrected version.
It will run these migrations in order based on the date of the migration. Note that running the db:migrate also invokes the db:schema:dump task, which will update your db/schema. rb file to match the structure of your database.
In Ruby on Rails, you can set default values for attributes in the database by including them as part of your migration. The syntax is default: 'value' . This is useful if you want to define lots of attributes at once, and it's easy to see what the default value is at a glance when looking at your db/schema. rb file.
Your last definition is correct. But the problem here isn't with Rails, but with PostgreSQL, which doesn't allow to add a column at specific position. Read more: How can I specify the position for a new column in PostgreSQL?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With