Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PostgreSQL: add_column "after" option usage in Rails migration

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 :)

like image 915
Joe Czucha Avatar asked Nov 30 '14 14:11

Joe Czucha


People also ask

How do you add references to migration?

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.

Which command is true to rollback migration in Rails?

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.

How does Rails know which migrations to run?

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.

How do I set default value in migration rails?

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.


1 Answers

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?

like image 135
Rustam A. Gasanov Avatar answered Oct 24 '22 05:10

Rustam A. Gasanov